refactor and cleanup save_database
openat should be used for saving as well
drop unnecessary debugging messages