Improved game loop. TODO : Code littered with global variables. Remove them.