Working algorithm for Switching