updated error handling logic