debugging issue where the script doesn't stop when it should