Reworked a lot of the querying.