Added logic to substring functions