Memory/off-by-one fixes