Code clean-up and minor optimizations. Also made sure that the current mode is always first.