fixed writing function