refactor using a function