some code from giraudet + carix -> matrix