Remove duplicated code

Introduce update_stats() and remove correspondng
duplicated code.

No functional change.
Fix a typo

Spotted by Isaac H. Dunn.

No functional change.
Merge remote-tracking branch 'upstream/master'