New version of this script, tested successfully