added code for tail recursion