Minor Bug in Writing function