Fix off-by-one issue with Memory and beforeId