Added some error handling for writing to closed channels