Refactoring internals to make things more testable