Fixed style issues: Used closure to leave global namespace unaffected. Raised variable declarations to top of functions, etc.