Make error handling more robust