Added flushing to database on shutdown