Added trie counter and debug output to avoid endless loop.