Added a lot of comments.

Did some catching up with the comments and cleaned up the code a bit.