Chapter 05, section 5.12, with experimental code refactoring.