reorganized code for TagGrammarFinder