I’m rather dissatisfied with the fix. It doesn’t fix the root cause. There’s an impedance mismatch between this function’s return “type” and the functions it calls. It should separate them into two variables. Resetting back to zero is precarious and will lead to the same bug being reintroduced.
Simple rule: Unless your function is a straight wrapper around another function, your return vale should not be settable by any function you call.