Comments and Cleanup

Added lots of comments explaining the code, and cleaned it up a little
bit.