Test and fix writing wide chars.
Move code from header to source.