Implementation of the game loop