Optimized the setMode method.