Refactor copy-paste-and-modifed code using callback.