Refactors code to remove duplication
Refactors test code