Removing tail recursion