alignment issues fixed
writeBinary rewritten with mallocForeignPtr