moved into one main loop