Code cleanup. Move database related functions to their own file. Add some comments.
Add a script to repack the database from the archives, in case of errors.