refined the error handling logic.