refactor context_tree