improved power-switching code