refactored code and got tests working