Bugfixes in Write function.