added error flow logic