Updated  error handling.