replace explicit recursion with higher order fns