Address some of gzm0's comments.
Factor out common code between optimize() and transform(Closure()).
Extract inlining of anon function in separate method.
Other comments by gzm0.