Modified script to handle environment problems