Fix up queries

Altering the queries, both to fix them (previous commit knowingly broke
them) and to allow for more precise data on what I'm looking for.
First half of this script

Fixed and final first half of this script; still needs a lot of work to
output useful data, but it's doing what I want (and prettily, to boot)