fixed off-by-one errors