* Some code is copied. Needs to be removed / re-written (just the write to file code snippet). * Can be more efficient with vectorization.