refactored logic for creating link, ready to merge