Yes, that was the fix, all of the rest was done to troubleshott what the issue. The fix was moving that line's position.
I honestly cant say why the code is placed in the wrong order.
In all of our testing the code is added correctly.
We need to find steps to be able to faithfully reproduce the error to look in the exact cause of the problem.