Fix and optimize device rotation