Small refactoring plus tests