fixed timing issue in query code