refactoring and reformatting

Performed some much needed cleanup and refactoring of the code, including
documenting some of the existing classes.