refactoring main loop, might save some battery