Code for reproducing work in Chapter 2