The good man from his good treasure brings forth good. The statement that from the abundance of the heart the mouth speaks he explains in particular. The word which proceeds from a thought is as a gift from a treasure. Hence if the thought is good, the word is good; and conversely. The good treasure is knowledge of the truth and fear of the Lord: "Abundance of salvation, wisdom and knowledge; the fear of the Lord is his treasure" (Is 33:6). Likewise, an evil treasure is evil thinking; and from this treasure only evil proceeds: "Treasures of wickedness do not profit" (Pr 10:2). Note: what is sa...id there of words is understood also of deeds. For as the thought is the wellspring of speech, so intention is of action. Therefore, if the intention is good, the action is good. Hence a Gloss: "You do according as you intend." This statement seems open to objection in regard to good. Suppose that someone wants to steal in order to give an alms: the act is evil and the intention good. Therefore... I answer that intending and willing are sometimes distinguished, namely, when in one and the same act the willing and the intention differ. Willing bears on the object wanted, but intention on the end. Willing, for example, is when I will to go to a window to see the people passing by; the latter is the intention. Hence it is necessary that willing and intending be one. Hence we can consider intention and willing in a wide sense, as in the assertion that if the willing is evil, the act is evil. Yet if it be excluded and taken in the proper sense, it is not true. But granted that intention and the act of willing are one, what then? I answer that the root of merit lies in charity, but consequently in the merit of other virtues. For merit looks at the essential reward, within which charity is considered. Thus, any work whatsoever that is performed with greater charity has more merit. But charity alone has God for object and end. Hence the merit of charity corresponds to the substantial reward, the merit of the other virtues to the accidental reward. Therefore, because charity permeates the intention, to the extent that one does something from greater charity to that extent he achieves; but in regard to accidental reward, this is not so.
Source: Commentary on Matthew