modify recursion and optimize