SQL polish, mostly working now, save for some future changes.