Refactored some logic code