I have been troubleshooting this script for weeks and finally found it was trying to divide by zero.