Added all functions to write the matrix file to disc. Still need to debug.