Added mode switching logic