MZ [Solved]actor.changeEquip nulls faceName on Optimize or Clear equipment

I tried to adapt my plugin EquipActor from MV to MZ, but after trying and making the adaptations, and a couple of code cleanings, in my tests, equiping or unequiping the piece of gear will work just as intended: Equip the piece of gear, the actor disappears, unequip it, the actor appears. But if you press optimize, even if there was no change at all, you will get that it tried to change gear to a inexistent actor.
Why is that?
Thread

In the default code it crashes in these functions:

Spoiler
Code:
Game_Actor.prototype.optimizeEquipments = function() {
    const maxSlots = this.equipSlots().length;
    this.clearEquipments();
    for (let i = 0; i < maxSlots; i++) {
        if (this.isEquipChangeOk(i)) {
            this.changeEquip(i, this.bestEquipItem(i)); //only line that makes the game flow on commented
        }
    }
};
Game_Actor.prototype.clearEquipments = function() {
    const maxSlots = this.equipSlots().length;
    for (let i = 0; i < maxSlots; i++) {
        if (this.isEquipChangeOk(i)) {
            this.changeEquip(i, null); //only line that makes the game flow on commented
        }
    }
};
Reproduction:



I left in the plugin instructions some of the things I tried and might help to solve the issue. I suggest using the demo so you save time setting things up.

[Edit]
I found out the issue. After clicking Optimize, or Clear, party members somehow become 9, starting from actor 1, ending in a null when using my plugin, and there's only 8 actors in the database and in the party.
screenshot.317.png

I edited the function "loadFaceImages" to a slower version to place these console logs.
[/Edit]
After so much investigating, I found out the error, in one of my code cleanings, I changed one of the return character to party if/else , so now it adds a null to the party.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-actor-changeequip-nulls-facename-on-optimize-or-clear-equipment.125718/