Refactor some late chapter 4 functions