finish main loop