Refactored writeMatrices.
Fixed up some confusion over directory and filenames in writeMatrix etc.