Merging from upstream
Adding comments, minor jslint cleanup and improvements for readability
Making debug logging more testable and adding tests