Query to condition refactor