Improve environment handling for scripts