Logic p2 - generating matrix

Generating and filling the matrix. Not QA'd / tested