Improves error handling and adds skeleton for async write function