Remove duplicated code

Introduce update_stats() and remove correspondng
duplicated code.

No functional change.
Merge branch 'master' into lmr_turbo
take 2
take 2