Improved error messages and error handling code