fix off-by-one errors