Changed the way the script determines when to stop running.  Maybe not as efficient, but hopefully will run correctly now.