refactoring
refactor to add recursive function with call back