my somewhat working version as of close to the end of Chapter 13