Programmer_P, the single biggest mistake you are doing is dumping extremely complicated code on us, asking us to debug it and trying to explain what it does in words. Stop.
If you have a problem, then show the smallest possible compilable example that demonstrates the problem. We don't care if it's an HTML and CSS generator, a car factory, a game or some animal breeder. It just doesn't matter.
If you have a problem, then you debug it. Can't find the error? Then start commenting out code until it works.
After doing all that, and you still don't understand what the problem is, then comment out all the code you can until the symptoms disappear.
Then re-write that code as simple as you can. You don't need some IAmSomeExtremelyLongAndComplicatedType and IAmAnExtremelyUnhelpfulTupe. A and B is just fine, and easier to read.
Then post the code here. Good code is self-explainable. You don't need to explain how it works--you shouldn't have to, as much as possible. It is far easier to read the code and find out what it does than read your explanations of what it does.
And I know this one sucks, and is typically boring, but it's still very good advice. Test everything you write properly before plugging it into your working code. Use test-driven development approaches!