This is sufficiently complex to earn its own unittest(s).