More sensible file writing; some tidy-up
Doc string