Unit tests for Write and bug fixes