main loop structure