Changed the write function to wait until all packages are send.