Crash fixes and efficient file writing