exercise 5.15 tails implemented