More robust error handling