Rewrite for new DB structure

I know there are lots of outstanding comments for your commits @alexnorton - I'm going to merge and then fix them, just easier.