Move packet creation to a separate function.