I just added this to my non-existent coding diary, and the specifics won't make much sense to anyone but me. But the experience would be immediately recognizable to anyone who has worked on a complex project for any length of time. As usual, ignore what you don't understand.
I recognized the pattern, there was spaghetti starting to envelop my new CMS code. This happens when you add special cases by adding boolean parameters to routines. That happens because when you wrote the high level code initially you didn't fully understand the problem.
The question is did you leave yourself the room in the schedule and with regard to potential breakage to actually rebuild the code so it correctly achieves its purpose? So when you read the code it looks like the pseudocode you'd write when conceiving it? If it's complicated enough and important enough -- it's worth the time to factor and re-factor until it reads like documentation of its functionality.
Too often you can't go back and re-engineer it, and you live with the bad construction. Then the code is fragile its whole life, so you don't build new layers on top of it, or if you do they're either inefficient or missing obviously valuable features.
I decided to rebuild this time. The problem was confusion over how and when index files would be built. What I realized was the code was optimized for minimizing the amount of stack building. When I got to that point in the rebuild, I added a cache, so the upper level code could be totally simple and natural. It worked!
Now the question is can I use the software for its intended purpose? I was able to do that before. Can I still do that?
See also: We Make Shitty Software.