I can't remember exactly how this script worked, but the changes seem to have done the trick.