improvements to recursive version