More refactoring and documentation