Move functions into more contained scope
Code cleanup for jshint