major refactoring of code and tests