added function to handle errors