Refactored to handle end of game