Refactoring of common code to generate message counts for each user