improved game loop logic