Fix count queries, +tests
Merge branch 'master' into forked_stuff