Lots of smallish changes to tweak functions, especially to make them
more abstract.