finish version3 (write to file directly)