Refactored a number of functions to make them learner and easier to read in game.js.