Refactor unlinking code

Implements advice from https://github.com/Zarel/Pokemon-Showdown/pull/886