Rearranged some code and comments