altered query cleanup, function count cat/ref