refactored badge logic code