Refactoring and more tests