Fix substring calculation.