refactoring and updated tests and code