Add querybuilder, add escaping, fix some ref problems, update tests.