Exercise 5.4, tail recursive version