Markus sim fixes.

This should be squashed into the previous commit before
cherry-picking it over to experimental or master.

Lots of whitespace changes, but also a number of real
fixes.