Semi-working query, added JUnit tests.