I wanted to mention two other aspects of this passage which may seem puzzling. In Exodus 20:5-6, God refers to Himself as "...a jealous God, punishing the children for the sin of the fathers to the third and fourth generation of those who hate me, but showing love to a thousand generations of those who love me and keep my commandments." It makes God sound like He is a jealous, 'smiting' type of God, which seems at odds with the idea of a loving God. However, we need to keep several things in mind. God and Israel had a covenant agreement, and, like the agreements between earthly kings and their subjects, Israel was expected to be totally loyal. Such covenants could be compared to a marriage, where exclusivity and love are expected. Like a husband or wife, God expected love and allegiance from His people. Anything threatening that relationship provoked His righteous jealousy. [Some Bible passages where the word 'jealous' is used could be translated 'zealous'; the same Hebrew word is used for both meanings. You need to understand the context in order to know which meaning is intended.] I think that part of our difficulty with this verse is that we always picture jealousy in a negative and selfish sense.
Secondly, although the same verses seem to indicate that God held grudges against the third and fourth generation descendants of those who 'hate' Him, this is not so. Households could consist of three or four generations, and these family members would be affected by the head of the household's blatant disregard for his covenant obligations toward God. While 'love' was a familiar covenant term for faithful allegiance, 'hate' was its opposite, a blatant rebellion against the LORD. For example, in Numbers chapter 16, Korah, Dathan and Abiram (as well as 250 followers) rebel against Moses and show contempt for the LORD by rejecting His rules regarding offerings -- and they and their households are swallowed up by the earth. Also, in Joshua chapter 7, when Achan stole some items which had been dedicated to the LORD, both he and his household were stoned and then destroyed by fire. Sin is extremely destructive, and often has tragic results. For example, a drunk driver might kill or injure other passengers or pedestrians.
On the other hand, it is amazing that those who do love the LORD and follow His ways can affect their families in a positive way. The LORD is described as "...showing love to a thousand generations of those who love me and keep my commandments." (Exodus 20:6) Although each individual must choose to obey or disobey the LORD, think of the various blessings which come upon the descendants of a godly person. Of course even believers sin, but think of the blessing of being brought up in a household which uses the Scriptures as a foundation for daily living and tries, even imperfectly, to live as God commands. Think of the compounding effect, in a relational sense, of those families who can put the other person's interests before their own, who shun gossip and outbursts of rage, who govern financial matters according to God's principles, and generally give evidence of the fruit of the Spirit [love,joy, peace, patience, kindness, goodness, faithfulness, gentleness and self-control] in countless areas of life. That is a treasure far beyond any earthly legacy.