main program loop reworked