lots of fixes for the main loop