Simplify query logic