refactoring for legibility rather than size

cleaned up code and ran through jshint
added comments
changed type detection methods (nodeType instead of instanceof
HTMLElement)