Refactor environment switch