Completed coding for Chapter 5