I know we generally avoid using multiple `var` declarations, but this is a case where it would really improve readability if some declarations were moved closer to their first uses. You've already done a great job making this method more readable, and that might help even more.

Giving the single-letter vars (except `i`) more descriptive names would also help.