Cleanup for  query structure