major refactoring and documentation