Off-by-one bug corrected