refactor a bit
Refine query method