Refactored some code in the link function