added tail recursion implementation