Refactored and centralized the location where mode is set and checked