modified main loop to be called from the main thread every frame