I had to refactor the center of this function into a private helper, because it was rapidly running out of horizontal space. Done.