added error handling and retry option