fixed writing to file as a large buffer