added safety check for queries that repeatedly match to stop looping