Better testing of query and some fixes