Complete code for NMA Chapter 27