Refactor to split message creation from writing, to support retries