Refactoring; added tests