Removed duplicate calls to rWriter.WriteHeader
Added the tests and fixed a few bugs