Added error handling logic