Refactoring and adding more tests