refactor using js closure