Some inline comments on what each variable is used for car improve readability of the logic that follows.