Improve optimizer

Most of the size improvements comes from the first commit. But a bit of it also comes from the second commit.
Same here?