Fix numerous bugs in environment setup script.