Integrate @mor1 patch to fix the mode selection