better validity check for query