Initial refactor for DRY and begin support for n environments rather than just 2