Various query fixes and a caching fix.