optimized recursion procedure