The only thing that might be considered new in Listing 2
The rationale for this computation is explained in the comments.