Improved Chapter 4 exercise 4-1