Refactored to enable higher order functions.