Handle your errors properly
https://beehaw.org/post/447667
I have been troubleshooting this script for weeks and finally found it was trying to divide by zero.