Reworked link insertion code