This looks like another case where Flip Matrix would be useful?
Without posting your code, we’re only guessing.
3. Attach minimal versions of all the relevant files