refacotring main loop