Explanation of the main loop routine