Added a better file writing function (v0.7)