More documentation and refactoring