rewrite only reachable nodes

I refined the implementation such that unreachable nodes are
discarded (in particular, nodes that become unreachable because of
rewriting).
refactoring