Substantially reworked code organization. Added smareter logic about setting up environments.