Various small cleanups

Some simplifications that are probably also optimizations, plus some formatting cleanup.