Fixed an off-by-one write bug, but need to fix the class replacement function