fix segmentaton fault, bad index