Fix issues in read and write paths. Now passes selftest.
Currently fails main_test.go, will be fixed in part II