Fixed off-by-one errors