using a trie-like approach