Improve badge number calculation