Add documentation and refactor some things