Refactor unlinking code