Separate metadata building from writing