Solved bug in matrix writing procedure