Avoiding int overflow when calculating index.