Documentation and minor refactoring