refactor and mode switch improvement