Refactoring in a functional style