optimization of recursion