--- device.c.orig	2006-05-19 17:18:40.000000000 +0200
+++ device.c	2006-05-20 08:26:37.000000000 +0200
@@ -282,6 +282,7 @@
 {
   cDevice *d = NULL;
   int select = INT_MAX;
+  int priStep = MAXPRIORITY+1;
 
   for (int i = 0; i < numDevices; i++) {
       bool ndr;
@@ -290,21 +291,21 @@
          if (device[i]->Receiving() && !ndr)
             pri = 0; // receiving and allows additional receivers
          else if (!device[i]->Receiving(true) && d && device[i]->ProvidesCa(Channel) < d->ProvidesCa(Channel))
-            pri = 1; // free and fewer Ca's
+            pri = priStep; // free and fewer Ca's
          else if (!device[i]->Receiving() && !device[i]->HasDecoder())
-            pri = 2; // free and not a full featured card
+            pri = 2*priStep; // free and not a full featured card
          else if (!device[i]->Receiving() && device[i] != ActualDevice())
-            pri = 3; // free and not the actual device
+            pri = 3*priStep; // free and not the actual device
          else if (!device[i]->Receiving() && !device[i]->IsPrimaryDevice())
-            pri = 4; // free and not the primary device
+            pri = 4*priStep; // free and not the primary device
          else if (!device[i]->Receiving())
-            pri = 5; // free
-         else if (d && device[i]->Priority() < d->Priority())
-            pri = 6; // receiving but priority is lower
+            pri = 5*priStep; // free
+         else if ((d && device[i]->Priority() < d->Priority()) || device[i]->Priority() < Priority)
+            pri = 6*priStep + device[i]->Priority(); // receiving but priority is lower
          else if (d && device[i]->Priority() == d->Priority() && device[i]->ProvidesCa(Channel) < d->ProvidesCa(Channel))
-            pri = 7; // receiving with same priority but fewer Ca's
+            pri = 7*priStep; // receiving with same priority but fewer Ca's
          else
-            pri = 8; // all others
+            pri = 8*priStep; // all others
          if (pri <= select) {
             select = pri;
             d = device[i];
