Split mainLoop into its own module