More refactoring and better documentation