Cleaning up documentation and code file by file.

Reordered functions in order of intended execution, and added JSDoc style documentation comments.