Improved mode selecting code