refactor and fix window switcher code