refactoring and adds some testing