fix size and off-by-one bug