make error handling more robust