Remove some code duplication using closures.