fixed off-by-one bug