Building matrix and printing functions