size-1 can be replaced by lastIndex.