Refactor: Move mode selection and debug mode to functions in separate file