Refactoring: new tests, separate functions