Fix game loop

I'm pretty sure I've caught every edge case, but I would appreciate someone else checking. I'm also not super happy that this doesn't sleep if it has the time to, but for desktop games that shouldn't really matter.