Fixing bug when appending to files