Index: branches/lemmings/code/behaviors/lemmings/lemmings.c
===================================================================
--- branches/lemmings/code/behaviors/lemmings/lemmings.c	(revision 304)
+++ branches/lemmings/code/behaviors/lemmings/lemmings.c	(revision 305)
@@ -17,14 +17,18 @@
 #define FOLLOW_MULTI_DEFAULT 1 // set default multi-following pattern
 #define SPEED 200 // set default speed
 
+#define LEMMINGS_PAN 7 // set the lemmings PAN
+
 /* Globals */
 static int cur_state; 		// current state
 static int local_leader;  // follow this bot (-1 if leader)
 static int global_leader; // this is the global leader
+static int follow_lock;  // this locks in the bot to follow
 static int follow_multi;	// set to 0 for single-file following, 1 for tree following
 static int local_id;      // this is the local robot id
 static int speed;         // robot's forward speed
 static int turn_speed;    // robot's turning speed
+static int pan;           // set the lemmings PAN
 
 /* Internal prototypes */
 static int get_local_leader(void);
@@ -46,9 +50,11 @@
   cur_state = FOLLOW;
   local_leader = -1;
   global_leader = -1;
+  follow_lock = 0;
   local_id = wl_get_xbee_id();
   speed = SPEED;
   turn_speed = 0;
+  pan = LEMMINGS_PAN;
 
   follow_multi = FOLLOW_MULTI_DEFAULT;
 }
@@ -76,7 +82,7 @@
   leader_check_count++;
   if (leader_check_count % 3 == 0) {
 
-    if (cur_state == LEAD && leader_check_count % 12 == 0) {
+    if (leader_check_count % 12 == 0) {
       // find robot with max id and assign as leader
       int max = -1;
       wl_token_iterator_begin();
@@ -94,6 +100,7 @@
         orb_set_color(BLUE);
         cur_state = LEAD;
         local_leader = -1;
+        follow_lock = 0;
         speed = SPEED;
         turn_speed = 0;
   //      usb_puts(" becoming leader \n");
@@ -113,35 +120,43 @@
       leader_bom = (local_leader>=0)?wl_token_get_sensor_reading(local_id, local_leader):-1;
 
       if (leader_bom < 0) {
-        //usb_puts("no connection to leader\n");
+        if (follow_lock) {
+          // unlock bot to follow
+          follow_lock = 0;
+          // actually change to follow other bot on next loop
+        }
+        else {
+          //usb_puts("no connection to leader\n");
 
-        // Can't see our old leader -- get new bot to follow.
-        local_leader = get_local_leader();
-        
-        if (local_leader == -1) {
-          /* if there is only one robot in the token ring, there is nothing to
-            follow, so don't do anything */
-          /*motors_off();
-          speed = 0;
-          return;*/
-          // nothing to follow, so become a local_leader and start leading
-          //usb_puts("can't see anyone - becoming a leader\n");
-          cur_state = LEAD;
-          local_leader = -1;
-          orb_set_color(BLUE);
-          speed = SPEED;
-          turn_speed = 0;
-        } else {
-          //usb_puts("new leader is");
-          //usb_puti(local_leader);
-          //usb_puts("\n");
-          // get data in order to follow leader
-          leader_bom = wl_token_get_sensor_reading(local_id, local_leader);
-          get_bom_velocity(leader_bom);
+          // Can't see our old leader -- get new bot to follow.
+          local_leader = get_local_leader();
+          
+          if (local_leader == -1) {
+            /* if there is only one robot in the token ring, there is nothing to
+              follow, so don't do anything */
+            /*motors_off();
+            speed = 0;
+            return;*/
+            // nothing to follow, so become a local_leader and start leading
+            //usb_puts("can't see anyone - becoming a leader\n");
+            cur_state = LEAD;
+            local_leader = -1;
+            orb_set_color(BLUE);
+            speed = SPEED;
+            turn_speed = 0;
+          } else {
+            //usb_puts("new leader is");
+            //usb_puti(local_leader);
+            //usb_puts("\n");
+            // get data in order to follow leader
+            leader_bom = wl_token_get_sensor_reading(local_id, local_leader);
+            get_bom_velocity(leader_bom);
+          } 
         }
       } else {
         // Leader is still in sight, so follow it
         get_bom_velocity(leader_bom);
+        follow_lock = 1; // lock in bot to follow
       }
     }
   }
@@ -225,6 +240,11 @@
   int cur_weight;
   int best_weight = 1000; // set to infinity initially
   int best_node = -1;
+  
+  // check for global leader first
+  int test = wl_token_get_sensor_reading(local_id, global_leader);
+  if (test != -1)
+    return global_leader;
 
   wl_token_iterator_begin();
 
