Improve metadata serialization and saving