if (sidmsg->calen == 0) {
     free_sidmsg(sidmsg);
+    msg->type = MSG_PROCESSED;
     return;
   }
   for(ch=Channels.First(); ch; ch=Channels.Next(ch)) {
         }
       }
       free_sidmsg(sidmsg);
+      msg->type = MSG_PROCESSED;
       return;
     }
   } else {
 
           pthread_mutex_unlock(&csa->state_lock);
           return;
         }
+        pthread_mutex_lock(&csa->keylock);
         if(! csa->keyindex[index].valid) {
           csa->keyindex[index].valid = 1;
           csa->keyindex[index].status = 0;
           csa->keyindex[index].queued = 0;
         }
+        pthread_mutex_unlock(&csa->keylock);
         pop_entry_from_queue_l(pid_ll, &pidmap_empty_queue, struct pid, &list_lock);
         pid_ll->pid = pid;
         pid_ll->index = index;
           ll_find_elem(pid_ll, csa->pid_map, index, index, struct pid);
           if(pid_ll == NULL) {
             //no valid pids on this index
+            pthread_mutex_lock(&csa->keylock);
             csa->keyindex[index].status = 0;
+            pthread_mutex_unlock(&csa->keylock);
             if(list_empty(&csa->pid_map)) {
               //state = ENCRYPTED_NOT_READY;
               csa->state = NOT_ENCRYPTED;
 
 {
   struct msgctrl *msgctrl;
   int priority;
+  int x;
   for (priority=0; priority <= MSG_HIGH_PRIORITY; priority++) {
     msgctrl = &message_control[priority];
     bzero(msgctrl, sizeof(struct msgctrl));
     INIT_LIST_HEAD(&msgctrl->msglist);
     INIT_LIST_HEAD(&msgctrl->empty_queue);
+    for (x = 0; x < 10; x++) {
+      struct msg *msg = (struct msg *)malloc(sizeof(struct msg));
+      list_add(&msg->list, &msgctrl->empty_queue);
+    }
     pthread_mutex_init(&msgctrl->mutex, NULL);
     pthread_cond_init(&msgctrl->cond, NULL);
   }
         list_del(&msg->list);
         break;
       }
+      if (!msg)
+        break;
       //If we've seen all elements on the queue, or the queue is empty,
       //we are done
       if(ptr == &msgctrl->msglist)
 
 
 bool cScSascDevice::SetCaDescr(ca_descr_t *ca_descr, bool initial)
 {
-  printf("Called cScSascDevice::SetCaDescr\n");
+  //printf("Called cScSascDevice::SetCaDescr\n");
   _SetCaDescr(cardIndex,ca_descr);
   return true;
 }
 
 bool cScSascDevice::SetCaPid(ca_pid_t *ca_pid)
 {
-  printf("Called cScSascDevice::SetCaPid\n");
+  //printf("Called cScSascDevice::SetCaPid\n");
   _SetCaPid(cardIndex,ca_pid);
   return true;
 }