Fix flapping test and simplify query