fwrite fix + rebased to master