Improve script robustness