Add documentation and refactoring