Bugfixes

Increased the sophistication of the logic by which the script sets
its environment.