Exercise 5.3, tail recursive version