rewrote to allow nonterminals