Add documentation and refactor