refactor environment setting up logic