Add dtm matrix and fix a lot of data transformation bugs