Test and fix for writeReplace