Added code and tests for chapter 4.