Clean up and fix queries