Cse rewrite

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