Fixed some off-by-one memory bugs, neatened things.