Refactoring on function