Minor changes to make script more robust