Optimized recursion.