fixed n off-by-one problem