Rearrange and comment code better. No functionality changes