What do you think about also using a recursive functor, as suggested by @tacticalgrace?