Improved error handling greatly