Moved variable declarations to the top of functions to prevent hoisting (http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html).