Build the main loop