Fixed a debug statement
Fixed safe write function