Fairly nasty copy-and-paste based approach to making this more transactional. (Again, this time branched from stable correctly.)