Various code cleanups.

Just putting this out for review since Magnus is working on a cleanup as well.