Clean up solution, add support for complex queries.