Fix logic error in error flag handling