added error handling logic