Refactoring, cleanup and documentation.