Refactoring, cleanup, and documentaiton