Fix index out of bounds error when calculating the end range of a bucket