Cleaned up query impl a bit