Added a bunch of code comments and did some code cleanup.