debugging off-by-one error