Fixed code for writing DB to file.