improved error handling