refactor code using closure