You may be able to refactor the common parts of both functions, and add a customizable function at the end.