added flush() after file write