Now the loaded document is one character short. Instead of subtracting 2 from the length, I added one. This is a temporary fix, I think I found the real problem.