improved Class 4 exercise 5 solution