More refactoring. Added documentation