made modification to handle errors to the original example code