fix out-of-bounds handling