much refactoring and documenting