some query tools and restructuring