Refactored to expose higher order functions and simplify use.