add tail-recursion to sum helper function