Added documentation to code.

Refactored the code somewhat, and added authorship and explanation
comments.