optimization of the tail recursion