Improved fix for off-by-one buffer overflow.