Minor tweaks to hopefully help people avoid some sharp corners. Patches 2 and 3 are optional, and have no functional change, but IMO make the code easier to follow for subsequent developers.