Refactoring using callback functions