Fix off-by-one end causing memory corruption