Code cleanup 2

Cleaned up and documented most of the rest of the code.