Reinvested dividends: yes, these are added to the basis. Dividends that you kept as cash do not impact the basis of the position. The (buy) commission is also part of your basis; the sell commission is subtracted from the proceeds. The total cost of shares divided by the total number of shares gives the average price per share.
If you experience a spinoff or stock sale of a subsidiary, the company should send instructions (possibly after the sale, if it's market-price driven) describing how to allocate basis to the two descendant companies. If you get cash value for a fractional share, that fractional share has a basis as well; remember to remove it from the position that liquidated the fraction. If on the other hand you experience an acquisition, your basis in the new company is the same as your basis in the old company, less the basis of any fractional share as cash.