DJ: refactored out some code duplication into reusable function.