Alright, i forgot a tiny thing, a DELETE WHERE gates = 0, easy to overlook. And i think i need to move a different thing to the right a few spots.
for revamping your code, turning it into 5ish queries from the numerous it was... and only having one tiny bug? this is the thanks i get?

Anyways, your gate val's are significantly off, because your gate return feature is off buddy.