main loop modified