refactor a function