One area that's easy to overlook when figuring your basis—particularly if you sell all your shares in a fund at once—is shares that you've acquired through automatic reinvestment.
When you purchase shares using reinvested dividends, it's as if the dividends were paid to you in cash, and then you immediately used that cash to purchase new shares.
The amount of the distribution that you use to purchase each full or fractional share is the original cost basis for that share.