Further documentation and refactoring