made error handling more robust