Just a thought, but with a method this big, and the way we use `descriptor` in the implementation, it's possible there's a good new class lurking in this code somewhere that'd make it more elegant.