Chapter 8 refactored.