Fixed write function, appending still not working