Refactor queries