fix off-by-one indexing issue