Fixed query problems and tests.