added latency switching code