Refactored GenerateWriteReference method (closes #44).