Move panel switching into a function