major cleanup and refactoring

cleaned up to pass jshint
added helpful comments
use safer type checking