tree-based rewrite part 1