Cse rewrite

A rewrite of common-subexpression-elimination. The old version was pretty flaky.