few fixes to the script

Hi Jeremy,
I fixed some issues with this script (92048fe and 8b56266). please check and confirm.
Cheers,
Anis