Refactored the trie code to make it easier to understand what is
actually happening.