Implement auto-snapshotting and queue flushing. fixes #5 fixes #2