Fix garbage writing bug