Remove the ugly hack and implement r/w correctly

I had big misunderstanding of how open/write/read calls work. Fixing
this finally got rid of the bug of 0-byte files and removal of an ugly
hack.