Reorder functions and improve comments