refactored the main loop thingey