Refactor plan rewriter