fix off-by-one bug