This turned out to be much more complicated than I thought.