few fixes to the script

Hi Jeremy,
I fixed some issues with this script. please check and confirm.
Cheers,
Anis