One of the first thing that one learns in software engineering is that self modifying code is pretty much a bad idea every time.
Spent 40 years designing and programming computer code. You are absolutely correct. Throw it out and go back to the drawing board. If I had a dollar for every time that truism proved to be true, I could have retired earlier.