This fixes some issues in mode switching handling.