Some documentation and refactoring