Chapter 8: Added error handling