Fixed tail-recursion bug