Refactor query production; fixes.