memory function off by one error removed