Fix off-by-one problem in g()