fixed off-by-one problem