improve main loop behavior