refactored link logic code to 3 pieces