Trie refactor