Reorder some functions and add comments. No functionality change.