Refactored metadata writing.