Code tidy and addressed JSLint warnings

Moved functions created in loops outside of the loop.
Commented every function
Commented variables where appropriate
Removed dead comments
Removed most dead code/variables.