Added code to handle more error scenarios