Documentation, cleanup and refactoring