Refactor window title swap code