refactor main loop to get seemingly better responsiveness