diff --git a/indra/llui/llcommandmanager.h b/indra/llui/llcommandmanager.h
index fdad7cd1b5a7acef14c26e689b6fce9e78b1d3f5..46e0fe6e69eb257c8b4d008fee7ebafc35a5b070 100644
--- a/indra/llui/llcommandmanager.h
+++ b/indra/llui/llcommandmanager.h
@@ -68,8 +68,8 @@ class LLCommandId
 		mUUID = LLUUID::generateNewID(p.name);
 	}
 
-	LLCommandId(const std::string& name, const LLUUID& uuid)
-	:	mName(name),
+	LLCommandId(const LLUUID& uuid)
+	:	mName(""),
 		mUUID(uuid)
 	{
 	}
diff --git a/indra/llui/lltoolbar.cpp b/indra/llui/lltoolbar.cpp
index 6332b2674ad31b099368e6d7ab5adf05b16425e7..776e91b7e5b95df50137866b4da74f6d31604ef6 100644
--- a/indra/llui/lltoolbar.cpp
+++ b/indra/llui/lltoolbar.cpp
@@ -114,8 +114,6 @@ LLToolBar::LLToolBar(const LLToolBar::Params& p)
 {
 	mButtonParams[LLToolBarEnums::BTNTYPE_ICONS_WITH_TEXT] = p.button_icon_and_text;
 	mButtonParams[LLToolBarEnums::BTNTYPE_ICONS_ONLY] = p.button_icon;
-	mDraggedCommand = LLCommandId::null;
-	mRank = 0;
 }
 
 LLToolBar::~LLToolBar()
@@ -211,19 +209,14 @@ bool LLToolBar::addCommand(const LLCommandId& commandId, int rank)
 	// Create the button and do the things that don't need ordering
 	LLToolBarButton* button = createButton(commandId);
 	mButtonPanel->addChild(button);
-	LLCommandId temp_command = commandId;
-	if (commandId.name() == "Drag Tool")
-	{
-		temp_command = LLCommandId("Drag Tool");
-	}
-	mButtonMap.insert(std::make_pair(temp_command.uuid(), button));
+	mButtonMap.insert(std::make_pair(commandId.uuid(), button));
 
 
 	// Insert the command and button in the right place in their respective lists
-	if ((rank >= mButtonCommands.size()) || (rank < 0))
+	if ((rank >= mButtonCommands.size()) || (rank == RANK_NONE))
 	{
 		// In that case, back load
-		mButtonCommands.push_back(temp_command);
+		mButtonCommands.push_back(commandId);
 		mButtons.push_back(button);
 	}
 	else 
@@ -238,7 +231,7 @@ bool LLToolBar::addCommand(const LLCommandId& commandId, int rank)
 			rank--;
 		}
 		// ...then insert
-		mButtonCommands.insert(it_command,temp_command);
+		mButtonCommands.insert(it_command,commandId);
 		mButtons.insert(it_button,button);
 	}
 
@@ -247,27 +240,28 @@ bool LLToolBar::addCommand(const LLCommandId& commandId, int rank)
 	return true;
 }
 
-bool LLToolBar::removeCommand(const LLCommandId& commandId)
+// Remove a command from the list
+// Returns the rank of the command in the original list so that doing addCommand(id,rank) right after
+// a removeCommand(id) would leave the list unchanged.
+// Returns RANK_NONE if the command is not found in the list
+int LLToolBar::removeCommand(const LLCommandId& commandId)
 {
-	if (!hasCommand(commandId)) return false;
+	if (!hasCommand(commandId)) return RANK_NONE;
 	
 	llinfos << "Merov debug : removeCommand, " << commandId.name() << ", " << commandId.uuid() << llendl;
 	// First erase the map record
-	LLCommandId temp_command = commandId;
-	if (commandId.name() == "Drag Tool")
-	{
-		temp_command = LLCommandId("Drag Tool");
-	}
-	command_id_map::iterator it = mButtonMap.find(temp_command.uuid());
+	command_id_map::iterator it = mButtonMap.find(commandId.uuid());
 	mButtonMap.erase(it);
 	
 	// Now iterate on the commands and buttons to identify the relevant records
+	int rank = 0;
 	std::list<LLToolBarButton*>::iterator it_button = mButtons.begin();
 	command_id_list_t::iterator it_command = mButtonCommands.begin();
-	while (*it_command != temp_command)
+	while (*it_command != commandId)
 	{
 		++it_button;
 		++it_command;
+		++rank;
 	}
 	
 	// Delete the button and erase the command and button records
@@ -277,7 +271,7 @@ bool LLToolBar::removeCommand(const LLCommandId& commandId)
 
 	mNeedsLayout = true;
 	
-	return true;
+	return rank;
 }
 
 void LLToolBar::clearCommandsList()
@@ -292,12 +286,7 @@ bool LLToolBar::hasCommand(const LLCommandId& commandId) const
 {
 	if (commandId != LLCommandId::null)
 	{
-		LLCommandId temp_command = commandId;
-		if (commandId.name() == "Drag Tool")
-		{
-			temp_command = LLCommandId("Drag Tool");
-		}
-		command_id_map::const_iterator it = mButtonMap.find(temp_command.uuid());
+		command_id_map::const_iterator it = mButtonMap.find(commandId.uuid());
 		return (it != mButtonMap.end());
 	}
 
@@ -310,12 +299,7 @@ bool LLToolBar::enableCommand(const LLCommandId& commandId, bool enabled)
 	
 	if (commandId != LLCommandId::null)
 	{
-		LLCommandId temp_command = commandId;
-		if (commandId.name() == "Drag Tool")
-		{
-			temp_command = LLCommandId("Drag Tool");
-		}
-		command_id_map::iterator it = mButtonMap.find(temp_command.uuid());
+		command_id_map::iterator it = mButtonMap.find(commandId.uuid());
 		if (it != mButtonMap.end())
 		{
 			it->second->setEnabled(enabled);
@@ -410,6 +394,10 @@ void LLToolBar::resizeButtonsInRow(std::vector<LLToolBarButton*>& buttons_in_row
 	}
 }
 
+// Returns the position of the coordinates as a rank in the button list. 
+// The rank is the position a tool dropped in (x,y) would assume in the button list.
+// The value returned is between 0 and mButtons.size(), 0 being the first element to the left
+// (or top) and mButtons.size() the last one to the right (or bottom).
 int LLToolBar::getRankFromPosition(S32 x, S32 y)
 {
 	int rank = 0;
@@ -618,12 +606,6 @@ void LLToolBar::draw()
 			}
 		}
 	}
-	// HACK!!!
-	if (!mDragAndDropTarget)
-	{
-		removeCommand(mDraggedCommand);
-		mDraggedCommand = LLCommandId::null;
-	}
 
 	updateLayoutAsNeeded();
 	// rect may have shifted during layout
@@ -654,12 +636,7 @@ void LLToolBar::createButtons()
 		LLToolBarButton* button = createButton(command_id);
 		mButtons.push_back(button);
 		mButtonPanel->addChild(button);
-		LLCommandId temp_command = command_id;
-		if (command_id.name() == "Drag Tool")
-		{
-			temp_command = LLCommandId("Drag Tool");
-		}
-		mButtonMap.insert(std::make_pair(temp_command.uuid(), button));
+		mButtonMap.insert(std::make_pair(command_id.uuid(), button));
 	}
 	mNeedsLayout = true;
 }
@@ -736,7 +713,7 @@ BOOL LLToolBar::handleDragAndDrop(S32 x, S32 y, MASK mask, BOOL drop,
 										EAcceptance* accept,
 										std::string& tooltip_msg)
 {
-	//llinfos << "Merov debug : handleDragAndDrop. drop = " << drop << ", x = " << x << ", y = " << y << llendl;
+	llinfos << "Merov debug : handleDragAndDrop. drop = " << drop << ", x = " << x << ", y = " << y << llendl;
 	// If we have a drop callback, that means that we can handle the drop
 	BOOL handled = (mHandleDropCallback ? TRUE : FALSE);
 	
@@ -761,19 +738,13 @@ BOOL LLToolBar::handleDragAndDrop(S32 x, S32 y, MASK mask, BOOL drop,
 			LLAssetType::EType type = inv_item->getType();
 			if (type == LLAssetType::AT_WIDGET)
 			{
-				mRank = getRankFromPosition(x, y);
-				mDraggedCommand = LLCommandId("Drag Tool",inv_item->getUUID());
-				removeCommand(mDraggedCommand);
-				addCommand(mDraggedCommand,mRank);
+				LLCommandId dragged_command(inv_item->getUUID());
+				int rank = getRankFromPosition(x, y);
+				removeCommand(dragged_command);
+				addCommand(dragged_command,rank);
 				mDragAndDropTarget = true;
 			}
 		}
-		else 
-		{
-			removeCommand(mDraggedCommand);
-			mDraggedCommand = LLCommandId::null;
-		}
-
 	}
 	
 	return handled;
diff --git a/indra/llui/lltoolbar.h b/indra/llui/lltoolbar.h
index 84fa7ec0df5b595ac075026d47f3dfc86c45e39e..56bc8b9bb3cccfb328f1b44c9ad673499d42b3ad 100644
--- a/indra/llui/lltoolbar.h
+++ b/indra/llui/lltoolbar.h
@@ -47,8 +47,8 @@ class LLToolBarButton : public LLButton
 public:
 	struct Params : public LLInitParam::Block<Params, LLButton::Params>
 	{
-		Optional<LLUI::RangeS32::Params>	button_width;
-		Optional<S32>						desired_height;
+		Optional<LLUI::RangeS32>	button_width;
+		Optional<S32>				desired_height;
 
 		Params()
 		:	button_width("button_width"),
@@ -161,9 +161,11 @@ class LLToolBar
 								   void* cargo_data,
 								   EAcceptance* accept,
 								   std::string& tooltip_msg);
+
+	static const int RANK_NONE = -1;
 	
-	bool addCommand(const LLCommandId& commandId, int rank = -1);
-	bool removeCommand(const LLCommandId& commandId);
+	bool addCommand(const LLCommandId& commandId, int rank = RANK_NONE);
+	int  removeCommand(const LLCommandId& commandId);		// Returns the rank the removed command was at, RANK_NONE if not found
 	bool hasCommand(const LLCommandId& commandId) const;
 	bool enableCommand(const LLCommandId& commandId, bool enabled);
 
@@ -184,8 +186,6 @@ class LLToolBar
 	tool_handledrag_callback_t		mHandleDragItemCallback;
 	tool_handledrop_callback_t		mHandleDropCallback;
 	bool							mDragAndDropTarget;
-	int								mRank;
-	LLCommandId						mDraggedCommand;
 
 public:
 	// Methods used in loading and saving toolbar settings
diff --git a/indra/newview/lltoolbarview.cpp b/indra/newview/lltoolbarview.cpp
index 44b244f16368c69f18729b8bf566fcca0a3e4d8f..8273d1491da29bd798969129de7398aed1bde435 100644
--- a/indra/newview/lltoolbarview.cpp
+++ b/indra/newview/lltoolbarview.cpp
@@ -41,7 +41,6 @@
 LLToolBarView* gToolBarView = NULL;
 
 static LLDefaultChildRegistry::Register<LLToolBarView> r("toolbar_view");
-bool LLToolBarView::sDragStarted = false;
 
 bool isToolDragged()
 {
@@ -331,18 +330,25 @@ void LLToolBarView::draw()
 
 void LLToolBarView::startDragTool( S32 x, S32 y, const LLUUID& uuid)
 {
-	//llinfos << "Merov debug: startDragTool() : x = " << x << ", y = " << y << llendl;
+	llinfos << "Merov debug: startDragTool() : x = " << x << ", y = " << y << ", uuid = " << uuid << llendl;
+	// Flag the tool dragging but don't start it yet
+	gToolBarView->mDragStarted = false;
+	gToolBarView->mDragCommand = LLCommandId::null;
+	gToolBarView->mDragRank = LLToolBar::RANK_NONE;
+	gToolBarView->mDragToolbar = NULL;
 	LLToolDragAndDrop::getInstance()->setDragStart( x, y );
-	sDragStarted = false;
 }
 
 BOOL LLToolBarView::handleDragTool( S32 x, S32 y, const LLUUID& uuid, LLAssetType::EType type)
 {
-//	llinfos << "Merov debug: handleDragTool() : x = " << x << ", y = " << y << ", uuid = " << uuid << llendl;
 	if (LLToolDragAndDrop::getInstance()->isOverThreshold( x, y ))
 	{
-		if (!sDragStarted)
+		if (!gToolBarView->mDragStarted)
 		{
+			llinfos << "Merov debug: handleDragTool() : x = " << x << ", y = " << y << ", uuid = " << uuid << llendl;
+			// Start the tool dragging:
+			
+			// First, create the global drag and drop object
 			std::vector<EDragAndDropType> types;
 			uuid_vec_t cargo_ids;
 			types.push_back(DAD_WIDGET);
@@ -350,9 +356,35 @@ BOOL LLToolBarView::handleDragTool( S32 x, S32 y, const LLUUID& uuid, LLAssetTyp
 			gClipboard.setSourceObject(uuid,LLAssetType::AT_WIDGET);
 			LLToolDragAndDrop::ESource src = LLToolDragAndDrop::SOURCE_VIEWER;
 			LLUUID srcID;
-			//llinfos << "Merov debug: handleDragTool() : beginMultiDrag()" << llendl;
 			LLToolDragAndDrop::getInstance()->beginMultiDrag(types, cargo_ids, src, srcID);
-			sDragStarted = true;
+			llinfos << "Merov debug: beginMultiDrag() launched" << llendl;
+			
+			// Second, check if the command is present in one of the 3 toolbars
+			// If it is, store the command, the toolbar and the rank in the toolbar and
+			// set a callback on end drag so that we reinsert the command if no drop happened
+			/*
+			gToolBarView->mDragCommand = LLCommandId(uuid);
+			if ((gToolBarView->mDragRank = gToolBarView->mToolbarLeft->removeCommand(gToolBarView->mDragCommand)) != LLToolBar::RANK_NONE)
+			{
+				gToolBarView->mDragToolbar = gToolBarView->mToolbarLeft;
+			}
+			else if ((gToolBarView->mDragRank = gToolBarView->mToolbarRight->removeCommand(gToolBarView->mDragCommand)) != LLToolBar::RANK_NONE)
+			{
+				gToolBarView->mDragToolbar = gToolBarView->mToolbarRight;
+			}
+			else if ((gToolBarView->mDragRank = gToolBarView->mToolbarBottom->removeCommand(gToolBarView->mDragCommand)) != LLToolBar::RANK_NONE)
+			{
+				gToolBarView->mDragToolbar = gToolBarView->mToolbarBottom;
+			}
+			if (gToolBarView->mDragRank != LLToolBar::RANK_NONE)
+			{
+				llinfos << "Merov debug: rank of dragged tool = " << gToolBarView->mDragRank << llendl;
+				LLToolDragAndDrop::getInstance()->setEndDragCallback(boost::bind(&LLToolBarView::onEndDrag, gToolBarView));
+			}
+			 */
+
+			llinfos << "Merov debug: Drag started cleanly" << llendl;
+			gToolBarView->mDragStarted = true;
 			return TRUE;
 		}
 		else
@@ -375,7 +407,7 @@ BOOL LLToolBarView::handleDropTool( void* cargo_data, S32 x, S32 y, LLToolBar* t
 		//llinfos << "Merov debug : handleDropTool. Drop source is a widget -> drop it in place..." << llendl;
 		// Get the command from its uuid
 		LLCommandManager& mgr = LLCommandManager::instance();
-		LLCommandId command_id("",inv_item->getUUID());
+		LLCommandId command_id(inv_item->getUUID());
 		LLCommand* command = mgr.getCommand(command_id);
 		if (command)
 		{
@@ -408,5 +440,19 @@ BOOL LLToolBarView::handleDropTool( void* cargo_data, S32 x, S32 y, LLToolBar* t
 
 void LLToolBarView::stopDragTool()
 {
-	sDragStarted = false;
+	// Clear the saved command, toolbar and rank
+	gToolBarView->mDragStarted = false;
+	gToolBarView->mDragCommand = LLCommandId::null;
+	gToolBarView->mDragRank = LLToolBar::RANK_NONE;
+	gToolBarView->mDragToolbar = NULL;
 }
+
+void LLToolBarView::onEndDrag()
+{
+	// If there's a saved command, reinsert it in the saved toolbar
+	if (gToolBarView->mDragRank != LLToolBar::RANK_NONE)
+	{
+		gToolBarView->mDragToolbar->addCommand(gToolBarView->mDragCommand,gToolBarView->mDragRank);
+	}
+	stopDragTool();
+}
\ No newline at end of file
diff --git a/indra/newview/lltoolbarview.h b/indra/newview/lltoolbarview.h
index a0c526ac541b694643a2440de775068faf3e468f..6623e63f8a92945a0bf33ecd60e38d450d293ea3 100644
--- a/indra/newview/lltoolbarview.h
+++ b/indra/newview/lltoolbarview.h
@@ -78,6 +78,7 @@ class LLToolBarView : public LLUICtrl
 	static BOOL handleDragTool( S32 x, S32 y, const LLUUID& uuid, LLAssetType::EType type);
 	static BOOL handleDropTool(	void* cargo_data, S32 x, S32 y, LLToolBar* toolbar);
 	static void stopDragTool();
+	void onEndDrag();
 	
 protected:
 	friend class LLUICtrlFactory;
@@ -94,12 +95,11 @@ class LLToolBarView : public LLUICtrl
 	LLToolBar*	mToolbarLeft;
 	LLToolBar*	mToolbarRight;
 	LLToolBar*	mToolbarBottom;
-	bool		mDragging;
-	LLToolBarButton* mDragButton;
-	S32			mMouseX;
-	S32			mMouseY;
 	
-	static bool			sDragStarted;
+	LLCommandId mDragCommand;
+	int			mDragRank;
+	LLToolBar*	mDragToolbar;
+	bool		mDragStarted;
 };
 
 extern LLToolBarView* gToolBarView;