Moved functions to the top to fix a jshint error