Improved error handling