Refactoring and adding in some tests