Remove n+1

some db optimizations mentioned in #386

most of the legwork was done by @phlipper. i simply did some cleanup and applied it to the newest master branch.