Replace with a non-recursive implementation?