Improved error handling and debug/error reporting