fixed writing to file