Added check to see if query is valid