Working version with mode switching