updated shell script to be more robust