Fix off-by-one problem