fixedsome sql bugs that were induced by previous refactor