separate where function
minor refactor builds query in stages