Refactoring and more testing