Refactoring with function