re-write to use a trie rather than bin search