Fix style inconsistencies & global leakage

Hey, thanks for writing this! I wanted to jump in and add support for exposing this library in the UMD format so that it works with AMD & CommonJS loaders as well, but before doing so, I noticed there were some inconsistencies in the code base. This PR fixes some of them, as well as addresses a few bugs where variables were being leaked globally and vars were redeclared. I tried to adhere to the style you were going for in my changes.