Added some logic to handle errors