Improved exercise 2.4 solution