Main program loop

updated main program loop.