Fixed off-by-one error in the code