Refactor env.sh moving all code into functions