main loop and getting rid of globals