max > nelem(stk) is wrong. should be >=; change to assert.