Reordering the main loop