diff --git a/scripts/ai/escort_player.lua b/scripts/ai/escort_player.lua
index 25406e4..b1a2dd9 100644
--- a/scripts/ai/escort_player.lua
+++ b/scripts/ai/escort_player.lua
@@ -4,6 +4,7 @@ include("../scripts/ai/tpl/escort.lua")
 armour_run    = 40
 armour_return = 70
 aggressive    = true
+command       = false
 
 function create()
   mem.escort = ai.getPlayer()
diff --git a/scripts/ai/tpl/escort.lua b/scripts/ai/tpl/escort.lua
index 92a0ff8..47f34d5 100644
--- a/scripts/ai/tpl/escort.lua
+++ b/scripts/ai/tpl/escort.lua
@@ -2,10 +2,11 @@ include("../scripts/ai/tpl/generic.lua") -- Simple create function.
 
 -- Shouldn't think, should only obey orders.
 atk_think = false
+command = true
 
 function create(master)
   mem.escort = master
-  mem.command = true -- On by default.
+  mem.carrier = true
   attack_choose()
 end
 
@@ -32,7 +33,7 @@ function escort()
     return
 
   -- Brake.
-  elseif dist < bdist then
+  elseif dist+100 < bdist then
     ai.pushtask(0, "brake")
 
   -- Must approach.
@@ -83,28 +84,28 @@ end
 --]]
 --Attack target.
 function e_attack(target)
-  if mem.command then
+  if command then
     ai.pushtask(0, "attack", target)
   end
 end
 
 -- Hold position.
 function e_hold()
-  if mem.command then
+  if command then
     ai.pushtask(0, "hold")
   end
 end
 
 -- Return to carrier.
 function e_return()
-  if mem.command and mem.carrier then
+  if command and mem.carrier then
     ai.pushtask(0, "flyback")
   end
 end
 
 -- Clear orders.
 function e_clear()
-  if mem.command then
+  if command then
     while ai.taskname() ~= "none" do
       ai.poptask()
     end
diff --git a/src/ai.c b/src/ai.c
index ce746d3..63b49b6 100644
--- a/src/ai.c
+++ b/src/ai.c
@@ -150,6 +150,7 @@ static int ai_getrndplanet(lua_State* L);       /* Vec2 getrndplanet() */
 static int ai_getlandplanet(lua_State* L);      /* Vec2 getlandplanet() */
 static int ai_hyperspace(lua_State* L);         /* [number] hyperspace() */
 static int ai_stop(lua_State* L);               /* stop() */
+static int ai_dock(lua_State* L);               /* dock(number) */
 /* Combat. */
 static int ai_combat(lua_State* L);             /* combat(number) */
 static int ai_settarget(lua_State* L);          /* settarget(number) */
@@ -207,6 +208,7 @@ static const luaL_Reg ai_methods[] = {
   { "brake",                ai_brake            },
   { "stop",                 ai_stop             },
   { "hyperspace",           ai_hyperspace       },
+  { "dock",                 ai_dock             },
   /* Combat. */
   { "aim",                  ai_aim              },
   { "combat",               ai_combat           },
@@ -529,8 +531,8 @@ void ai_think(Pilot* pilot) {
             cur_pilot->solid->dir);
 
   /* Fire weapons if needs be. */
-  if(ai_isFlag(AI_PRIMARY)) pilot_shoot(pilot, 0); /* Primary. */
-  if(ai_isFlag(AI_SECONDARY)) pilot_shoot(pilot, 1); /* Secondary. */
+  if(ai_isFlag(AI_PRIMARY)) pilot_shoot(cur_pilot, 0); /* Primary. */
+  if(ai_isFlag(AI_SECONDARY)) pilot_shoot(cur_pilot, 1); /* Secondary. */
 }
 
 /**
@@ -1116,6 +1118,21 @@ static int ai_stop(lua_State* L) {
   return 0;
 }
 
+/* Docks the ship. */
+static int ai_dock(lua_State* L) {
+  Pilot* p;
+
+  /* Target is another ship. */
+  if(lua_isnumber(L, 1)) {
+    p = pilot_get(lua_tonumber(L, 1));
+    if(p == NULL) return 0;
+    pilot_dock(cur_pilot, p);
+  }
+  else LLUA_INVALID_PARAMETER();
+
+  return 0;
+}
+
 /* Aim at the pilot, trying to hit it. */
 static int ai_aim(lua_State* L) {
   int id;
diff --git a/src/escort.c b/src/escort.c
index 93a9b64..5babe4f 100644
--- a/src/escort.c
+++ b/src/escort.c
@@ -98,7 +98,7 @@ static int escort_command(Pilot* parent, int cmd, int param) {
       continue;
 
     /* Check if command makes sense. */
-    if((cmd == ESCORT_RETURN) && !pilot_isFlag(parent, PILOT_CARRIED))
+    if((cmd == ESCORT_RETURN) && !pilot_isFlag(e, PILOT_CARRIED))
       continue;
     
     n++; /* Amount of escorts left. */
diff --git a/src/outfit.h b/src/outfit.h
index aefc3dc..d9b1267 100644
--- a/src/outfit.h
+++ b/src/outfit.h
@@ -15,9 +15,9 @@
  */
 typedef enum OutfitType_ {
   OUTFIT_TYPE_NULL,                       /**< NULL type. */
-  OUTFIT_TYPE_BOLT,                       /**< @todo Fixed bolt cannon. */
+  OUTFIT_TYPE_BOLT,                       /**< Fixed bolt cannon. */
   OUTFIT_TYPE_BEAM,                       /**< Fixed beam cannon. */
-  OUTFIT_TYPE_TURRET_BOLT,                /**< @todo Rotaty bolt turret. */
+  OUTFIT_TYPE_TURRET_BOLT,                /**< Rotaty bolt turret. */
   OUTFIT_TYPE_TURRET_BEAM,                /**< Rotary bolt turret. */
   OUTFIT_TYPE_MISSILE_DUMB,               /**< Dumb missile launcher. */
   OUTFIT_TYPE_MISSILE_DUMB_AMMO,          /**< Dumb missile ammo. */
diff --git a/src/pilot.c b/src/pilot.c
index 30af198..593431f 100644
--- a/src/pilot.c
+++ b/src/pilot.c
@@ -580,6 +580,40 @@ void pilot_setAfterburner(Pilot* p) {
   p->afterburner = NULL;
 }
 
+/**
+ * @fn int pilot_dock(Pilot* p, Pilot* target)
+ *
+ * @brief Dock the pilot on its target pilot.
+ *    @param p Pilot that wants to dock.
+ *    @param target Pilot to dock on.
+ *    @return 0 on successful docking.
+ */
+int pilot_dock(Pilot* p, Pilot* target) {
+  int i;
+  Outfit* o;
+
+  /* Check to see if target has an available bay. */
+  for(i = 0; i < target->noutfits; i++) {
+    if(outfit_isFighterBay(target->outfits[i].outfit)) {
+      o = outfit_get(outfit_ammo(target->outfits[i].outfit));
+      if(outfit_isFighter(o) &&
+          (strcmp(p->ship->name, o->u.fig.ship)==0))
+        break;
+    }
+  }
+  if(i >= target->noutfits)
+    return -1;
+
+  /* Add the pilots outfit. */
+  if(pilot_addOutfit(target, o, 1) != 1)
+    return -1;
+
+  /* Destroy the pilot. */
+  pilot_setFlag(p, PILOT_DELETE);
+
+  return 0;
+}
+
 /**
  */
 void pilot_explode(double x, double y, double radius,
@@ -1283,9 +1317,9 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, int faction,
 
   /* Escort stuff. */
   if(flags & PILOT_ESCORT) {
-    pilot->flags |= PILOT_ESCORT;
+    pilot_setFlag(pilot, PILOT_ESCORT);
     if(flags & PILOT_CARRIED)
-      pilot->flags |= PILOT_CARRIED;
+      pilot_setFlag(pilot, PILOT_CARRIED);
   }
 
   /* AI. */
diff --git a/src/pilot.h b/src/pilot.h
index d62cff9..b5b6be4 100644
--- a/src/pilot.h
+++ b/src/pilot.h
@@ -235,6 +235,8 @@ void pilot_setSecondary(Pilot* p, const char* secondary);
 void pilot_setAmmo(Pilot* p);
 int pilot_getAmmo(Pilot* p, Outfit* o);
 void pilot_setAfterburner(Pilot* p);
+/* Escort stuff. */
+int pilot_dock(Pilot* p, Pilot* target);
 /* Explosion. */
 void pilot_explode(double x, double y, double radius,
     DamageType dtype, double damage, unsigned int parent);