Rearranged code, edited comments.
Minor fixes