Fix script environment bugs