[SOLVED] Help with iptables custom script

I am running Openwrt 15.05.1 (LEDE 17.01.4 was causing CPU usage issues so I had to go back) with ExtRoot on TP-Link TL-MR3420 v2 and I am gathering data for internet usage. I have a custom script which worked fine before with LEDE but now sometimes it does not create all of the rules or creates them multiple times. Maybe someone can look up and tell me if I can improve it so it doesn't create any problems for me. Thanks
This is the code: https://pastebin.com/4GPYAx04

See the below pics...


I've had this issue with previous versions of OpenWRT. I'm not sure a solution was ever offered to me. In my case, I was processing a list of over 400 IP addresses. I switched to ipset to fix that. The other rules I placed into the UCI.

Quite a few of these rules could be placed into the UCI. Why haven't you done so?

OpenWRT was designed to use the FW3 firewall scripts - and they use UCI. Your bootup is likely freezing.

Well I have made some changes so it doesn't need to check each of the rule to determine if the rule exists. I am now just checking if the chain exists or not and create or flush the rules accordingly. I think that should help the freezing problem.