Optimized codes to saving storage and memory

Should be able to be further improved. Intermediate output files are
still too large for very larger matrix.