Fixed code to handle cases that would break the trie