modified to convert to matrix