diff --git a/indra/newview/llagent.cpp b/indra/newview/llagent.cpp
index 29c1030e60609497d56df412c5dd9e382f7e993a..0b9ee71458e7ddbe04ed86ed89d3e5d95271117c 100644
--- a/indra/newview/llagent.cpp
+++ b/indra/newview/llagent.cpp
@@ -77,8 +77,9 @@
 #include "llwindow.h"
 #include "llworld.h"
 #include "llworldmap.h"
-// [RLVa:KB] - Checked: 2010-03-07 (RLVa-1.2.0c)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvhelper.h"
 // [/RLVa:KB]
 
 using namespace LLVOAvatarDefines;
diff --git a/indra/newview/llagentlistener.cpp b/indra/newview/llagentlistener.cpp
index 66f3853e792d4f5dc36fc85526f1e13ca0ca3f2a..f9bdc6c44a7dbf8f21d93d9f73f8a8052b5a4839 100644
--- a/indra/newview/llagentlistener.cpp
+++ b/indra/newview/llagentlistener.cpp
@@ -37,8 +37,9 @@
 #include "llviewerobject.h"
 #include "llviewerobjectlist.h"
 #include "llviewerregion.h"
-// [RLVa:KB] - Checked: 2010-03-06 (RLVa-1.2.0c)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "llvoavatarself.h"
 // [/RLVa:KB]
 
 LLAgentListener::LLAgentListener(LLAgent &agent)
diff --git a/indra/newview/llagentwearables.cpp b/indra/newview/llagentwearables.cpp
index 94df06ced7eeaf38e86c5b917ae7566b1cc08088..805930baa415580850f2203f05a217b069c9e0bc 100644
--- a/indra/newview/llagentwearables.cpp
+++ b/indra/newview/llagentwearables.cpp
@@ -49,8 +49,9 @@
 #include "llvoavatarself.h"
 #include "llwearable.h"
 #include "llwearablelist.h"
-// [RLVa:KB] - Checked: RLVa-1.2.0a (2010-03-04)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 #include <boost/scoped_ptr.hpp>
diff --git a/indra/newview/llappearancemgr.cpp b/indra/newview/llappearancemgr.cpp
index b6dbdadfb5fc9ea507bf00b1de418a87f93db017..5e9bf72196ef686fe40eea695585cf9b72847c08 100644
--- a/indra/newview/llappearancemgr.cpp
+++ b/indra/newview/llappearancemgr.cpp
@@ -49,8 +49,10 @@
 #include "llvoavatarself.h"
 #include "llviewerregion.h"
 #include "llwearablelist.h"
-// [RLVa:KB] - Checked: 2010-03-05 (RLVa-1.2.0b)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvhelper.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 // RAII thingy to guarantee that a variable gets reset when the Setter
diff --git a/indra/newview/llattachmentsmgr.cpp b/indra/newview/llattachmentsmgr.cpp
index 56490162b1983fad422437e9a5e2f76155084427..8a74fd5d87fb87975aacf29dcbaa0134378f8dd0 100644
--- a/indra/newview/llattachmentsmgr.cpp
+++ b/indra/newview/llattachmentsmgr.cpp
@@ -33,8 +33,9 @@
 #include "llviewerinventory.h"
 #include "llviewerregion.h"
 #include "message.h"
-// [RLVa:KB] - Checked: 2010-09-13 (RLVa-1.2.1c)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 LLAttachmentsMgr::LLAttachmentsMgr()
diff --git a/indra/newview/llfloaterinspect.cpp b/indra/newview/llfloaterinspect.cpp
index dd25e3a52f0ac146518aa46cdc337968e61ede45..2e5efdbeee1ee388e92fe22fec94cfaae7ef1101 100644
--- a/indra/newview/llfloaterinspect.cpp
+++ b/indra/newview/llfloaterinspect.cpp
@@ -40,8 +40,9 @@
 #include "llviewercontrol.h"
 #include "llviewerobject.h"
 #include "lluictrlfactory.h"
-// [RLVa:KB] - Checked: 2010-08-25 (RLVa-1.2.2a)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "llagent.h"
 // [/RLVa:KB]
 
 //LLFloaterInspect* LLFloaterInspect::sInstance = NULL;
diff --git a/indra/newview/llinventorybridge.cpp b/indra/newview/llinventorybridge.cpp
index eb84b69e153699238880926f17b518a9451862f3..97061ae0249ea1534fb1d6855710e04bf862e74e 100644
--- a/indra/newview/llinventorybridge.cpp
+++ b/indra/newview/llinventorybridge.cpp
@@ -68,8 +68,9 @@
 #include "llviewerwindow.h"
 #include "llvoavatarself.h"
 #include "llwearablelist.h"
-// [RLVa:KB] - Checked: 2010-02-27 (RLVa-1.2.0b)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 typedef std::pair<LLUUID, LLUUID> two_uuids_t;
diff --git a/indra/newview/llinventoryfunctions.cpp b/indra/newview/llinventoryfunctions.cpp
index 18dcc865b7adbfcaaed8443d6865758844235554..2af361a422dfbbff0a2569c8dd02a5fafc30461d 100644
--- a/indra/newview/llinventoryfunctions.cpp
+++ b/indra/newview/llinventoryfunctions.cpp
@@ -80,8 +80,9 @@
 #include "llviewerwindow.h"
 #include "llvoavatarself.h"
 #include "llwearablelist.h"
-// [RLVa:KB] - Checked: 2010-11-30 (RLVa-1.3.0b) | Added: RLVa-1.3.0b
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 BOOL LLInventoryState::sWearNewClothing = FALSE;
diff --git a/indra/newview/llinventorymodel.cpp b/indra/newview/llinventorymodel.cpp
index 430d5a103eca34665cea1f5c4ae3412d9bf7662d..d3fca200af3b11c9d795119f5e10651966f681bc 100644
--- a/indra/newview/llinventorymodel.cpp
+++ b/indra/newview/llinventorymodel.cpp
@@ -46,8 +46,9 @@
 #include "llviewerregion.h"
 #include "llcallbacklist.h"
 #include "llvoavatarself.h"
-// [RLVa:KB] - Checked: RLVa-1.2.0a (2010-03-05)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 //#define DIFF_INVENTORY_FILES
diff --git a/indra/newview/llpanelcontents.cpp b/indra/newview/llpanelcontents.cpp
index 290d76333af9535cfccd3146e3f6b5e87c5b4e5f..47425c3f19e17dc5ed9d622e51439a9adbee4915 100644
--- a/indra/newview/llpanelcontents.cpp
+++ b/indra/newview/llpanelcontents.cpp
@@ -59,8 +59,9 @@
 #include "llviewerregion.h"
 #include "llviewerwindow.h"
 #include "llworld.h"
-// [RLVa:KB] - Checked: 2010-03-31 (RLVa-1.2.0c)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 //
diff --git a/indra/newview/llpanelobject.cpp b/indra/newview/llpanelobject.cpp
index 9e1b78656b649069fab582e9f768e248b812b750..6e19c342416f479b6cd33ffd104afca9c2e288cb 100644
--- a/indra/newview/llpanelobject.cpp
+++ b/indra/newview/llpanelobject.cpp
@@ -68,8 +68,9 @@
 #include "llviewercontrol.h"
 #include "lluictrlfactory.h"
 //#include "llfirstuse.h"
-// [RLVa:KB] - Checked: 2010-03-31 (RLVa-1.2.0c)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "llvoavatarself.h"
 // [/RLVa:KB]
 
 #include "lldrawpool.h"
diff --git a/indra/newview/llpanelobjectinventory.cpp b/indra/newview/llpanelobjectinventory.cpp
index 1fbfea01c1e71f41c5c3e445b73729fabb54ed2f..8dfa2531b0463bd22da236d9f105ba6f1da4b7c9 100644
--- a/indra/newview/llpanelobjectinventory.cpp
+++ b/indra/newview/llpanelobjectinventory.cpp
@@ -63,8 +63,9 @@
 #include "llviewerregion.h"
 #include "llviewerobjectlist.h"
 #include "llviewermessage.h"
-// [RLVa:KB] - Checked: 2010-03-27 (RLVa-1.2.0b)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 ///----------------------------------------------------------------------------
diff --git a/indra/newview/llpreviewscript.cpp b/indra/newview/llpreviewscript.cpp
index 0d71da82b7462203b5ab89365a2670ad2307438a..5d1d49a7fc1d22cb4f66cc47959d4ff786ca1988 100644
--- a/indra/newview/llpreviewscript.cpp
+++ b/indra/newview/llpreviewscript.cpp
@@ -85,8 +85,9 @@
 #include "lltrans.h"
 #include "llviewercontrol.h"
 #include "llappviewer.h"
-// [RLVa:KB] - Checked: 2010-09-28 (RLVa-1.2.1f)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 const std::string HELLO_LSL =
diff --git a/indra/newview/llselectmgr.cpp b/indra/newview/llselectmgr.cpp
index 9eef7abd70bd9e1673dfea4e964c106daa40946e..7f067a8358f938ae5cdaa9fcc8a3e9bbb2fa44ad 100644
--- a/indra/newview/llselectmgr.cpp
+++ b/indra/newview/llselectmgr.cpp
@@ -88,7 +88,7 @@
 #include "llvoavatarself.h"
 #include "llvovolume.h"
 #include "pipeline.h"
-// [RLVa:KB] - Checked: 2010-03-23 (RLVa-1.2.0a)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
 // [/RLVa:KB]
 
diff --git a/indra/newview/lltooldraganddrop.cpp b/indra/newview/lltooldraganddrop.cpp
index 60c201c1667c05c45ccfb6da2cb6dbc34ac9533d..79886241a32cb9ee514f2d18b4210b2d9702a9b3 100644
--- a/indra/newview/lltooldraganddrop.cpp
+++ b/indra/newview/lltooldraganddrop.cpp
@@ -58,8 +58,9 @@
 #include "llviewerwindow.h"
 #include "llvoavatarself.h"
 #include "llworld.h"
-// [RLVa:KB] - Checked: 2010-03-04 (RLVa-1.2.0a)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 // syntactic sugar
diff --git a/indra/newview/llviewerdisplay.cpp b/indra/newview/llviewerdisplay.cpp
index 11f311e9b0fa9e737039da002c8c4c5c87608e3e..983a8d48ff68fd4034ed4a206c3b6e12dda43a27 100644
--- a/indra/newview/llviewerdisplay.cpp
+++ b/indra/newview/llviewerdisplay.cpp
@@ -77,8 +77,9 @@
 #include "llwlparammanager.h"
 #include "llwaterparammanager.h"
 #include "llpostprocess.h"
-// [RLVa:KB] - Checked: 2010-08-22 (RLVa-1.2.1a)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 extern LLPointer<LLViewerTexture> gStartTexture;
diff --git a/indra/newview/llviewermenu.cpp b/indra/newview/llviewermenu.cpp
index 2957a60da20b8187320ecbd492ad6e01f611098d..ed07348746a0e97d17241b5f32ed0d185fc46dfb 100644
--- a/indra/newview/llviewermenu.cpp
+++ b/indra/newview/llviewermenu.cpp
@@ -105,9 +105,9 @@
 #include "lltrans.h"
 #include "lleconomy.h"
 #include "boost/unordered_map.hpp"
-
-// [RLVa:KB] - Checked: 2010-03-09 (RLVa-1.2.0a)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 using namespace LLVOAvatarDefines;
diff --git a/indra/newview/llviewerobject.cpp b/indra/newview/llviewerobject.cpp
index db0df4900c01a1a8d7117b074748566c41911cf2..5b923ff9aa117d39b78c32126d0ac0ca4487f9ef 100644
--- a/indra/newview/llviewerobject.cpp
+++ b/indra/newview/llviewerobject.cpp
@@ -100,8 +100,9 @@
 #include "lltrans.h"
 #include "llsdutil.h"
 #include "llmediaentry.h"
-// [RLVa:KB] - Checked: 2010-03-27 (RLVa-1.2.0b)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 //#define DEBUG_UPDATE_TYPE
diff --git a/indra/newview/llvoavatarself.cpp b/indra/newview/llvoavatarself.cpp
index b1231bce3639991630de63f7b116aa0687312b75..87ab9afcf7a38c673463760214cac0e5aa60d32e 100644
--- a/indra/newview/llvoavatarself.cpp
+++ b/indra/newview/llvoavatarself.cpp
@@ -56,8 +56,9 @@
 #include "llviewerstats.h"
 #include "llviewerregion.h"
 #include "llappearancemgr.h"
-// [RLVa:KB] - Checked: 2010-03-05 (RLVa-1.2.0a)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 #if LL_MSVC
diff --git a/indra/newview/llvovolume.cpp b/indra/newview/llvovolume.cpp
index ab26585647d351c8820e9684e289278592b552fe..d4c21251068c5057cf04a3105f9768a1e8e0790b 100644
--- a/indra/newview/llvovolume.cpp
+++ b/indra/newview/llvovolume.cpp
@@ -63,8 +63,9 @@
 #include "llmediadataclient.h"
 #include "llagent.h"
 #include "llviewermediafocus.h"
-// [RLVa:KB] - Checked: 2010-04-04 (RLVa-1.2.0d)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 const S32 MIN_QUIET_FRAMES_COALESCE = 30;
diff --git a/indra/newview/llwearableitemslist.cpp b/indra/newview/llwearableitemslist.cpp
index 9c68d6e9ca30eca166c4e64bb522a9aeea7d5a35..6ebf5eac5c89fc7ec8ee64c2f63e317e8aa9cc9e 100644
--- a/indra/newview/llwearableitemslist.cpp
+++ b/indra/newview/llwearableitemslist.cpp
@@ -37,8 +37,9 @@
 #include "lltransutil.h"
 #include "llviewerattachmenu.h"
 #include "llvoavatarself.h"
-// [RLVa:KB] - Checked: 2010-09-04 (RLVa-1.2.1a)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 class LLFindOutfitItems : public LLInventoryCollectFunctor
diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index 9ff074716655aac76d1fd5c9ff4a8ab5f86a1723..abd2b384919d330369e280e428b4d600482df23f 100644
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -100,8 +100,9 @@
 #include "llspatialpartition.h"
 #include "llmutelist.h"
 #include "lltoolpie.h"
-// [RLVa:KB] - Checked: 2010-04-04 (RLVa-1.2.0d)
+// [RLVa:KB] - Checked: 2011-05-22 (RLVa-1.3.1a)
 #include "rlvhandler.h"
+#include "rlvlocks.h"
 // [/RLVa:KB]
 
 #ifdef _DEBUG
diff --git a/indra/newview/rlvcommon.cpp b/indra/newview/rlvcommon.cpp
index 204a3f1ff103cbf4f06fd367f29a3b545ec497c7..8b32eedfef3b9f99b51a5e41db09cb771e0f6cf7 100644
--- a/indra/newview/rlvcommon.cpp
+++ b/indra/newview/rlvcommon.cpp
@@ -29,6 +29,7 @@
 #include "llworld.h"
 
 #include "rlvcommon.h"
+#include "rlvhelper.h"
 #include "rlvhandler.h"
 #include "rlvlocks.h"
 
@@ -522,6 +523,32 @@ bool rlvMenuEnableIfNot(const LLSD& sdParam)
 // Selection functors
 //
 
+// Checked: 2010-04-11 (RLVa-1.2.0b) | Modified: RLVa-0.2.0g
+bool rlvCanDeleteOrReturn()
+{
+	bool fIsAllowed = true;
+
+	if (gRlvHandler.hasBehaviour(RLV_BHVR_REZ))
+	{
+		// We'll allow if none of the prims are owned by the avie or group owned
+		LLObjectSelectionHandle handleSel = LLSelectMgr::getInstance()->getSelection();
+		RlvSelectIsOwnedByOrGroupOwned f(gAgent.getID());
+		if ( (handleSel.notNull()) && ((0 == handleSel->getRootObjectCount()) || (NULL != handleSel->getFirstRootNode(&f, FALSE))) )
+			fIsAllowed = false;
+	}
+	
+	if ( (gRlvHandler.hasBehaviour(RLV_BHVR_UNSIT)) && (isAgentAvatarValid()) )
+	{
+		// We'll allow if the avie isn't sitting on any of the selected objects
+		LLObjectSelectionHandle handleSel = LLSelectMgr::getInstance()->getSelection();
+		RlvSelectIsSittingOn f(gAgentAvatarp->getRoot());
+		if ( (handleSel.notNull()) && (handleSel->getFirstRootNode(&f, TRUE)) )
+			fIsAllowed = false;
+	}
+
+	return fIsAllowed;
+}
+
 // Checked: 2010-04-20 (RLVa-1.2.0f) | Modified: RLVa-0.2.0f
 bool RlvSelectHasLockedAttach::apply(LLSelectNode* pNode)
 {
@@ -609,6 +636,18 @@ bool rlvPredCanNotRemoveItem(const LLViewerInventoryItem* pItem)
 	return !rlvPredCanRemoveItem(pItem);
 }
 
+// Checked: 2010-04-24 (RLVa-1.2.0f) | Added: RLVa-1.2.0f
+RlvPredIsEqualOrLinkedItem::RlvPredIsEqualOrLinkedItem(const LLUUID& idItem)
+{
+	m_pItem = gInventory.getItem(idItem);
+}
+
+// Checked: 2010-04-24 (RLVa-1.2.0f) | Added: RLVa-1.2.0f
+bool RlvPredIsEqualOrLinkedItem::operator()(const LLViewerInventoryItem* pItem) const
+{
+	return (m_pItem) && (pItem) && (m_pItem->getLinkedUUID() == pItem->getLinkedUUID());
+}
+
 // ============================================================================
 // Various public helper functions
 //
diff --git a/indra/newview/rlvcommon.h b/indra/newview/rlvcommon.h
index 5df06803b24fa0661b641caf97638fc0a68ac9aa..7c735dfa68b1dc2c06ed0a5e3483549cfe572b6e 100644
--- a/indra/newview/rlvcommon.h
+++ b/indra/newview/rlvcommon.h
@@ -18,21 +18,44 @@
 #define RLV_COMMON_H
 
 #include "llavatarname.h"
-#include "llinventorymodel.h"
 #include "llselectmgr.h"
 #include "llviewercontrol.h"
-#include "llviewerinventory.h"
 
 #include "rlvdefines.h"
 
+#ifdef LL_WINDOWS
+	#pragma warning (push)
+	#pragma warning (disable : 4702) // warning C4702: unreachable code
+#endif
+#include <boost/variant.hpp>
+#ifdef LL_WINDOWS
+	#pragma warning (pop)
+#endif
+
 // ============================================================================
 // Forward declarations
 //
 
+//
+// General viewer source
+//
+class LLInventoryItem;
+class LLViewerInventoryCategory;
+class LLViewerInventoryItem;
+class LLViewerJointAttachment;
+
+//
+// RLVa-specific
+//
 class RlvCommand;
+typedef std::list<RlvCommand> rlv_command_list_t;
+class RlvObject;
+
+struct RlvException;
+typedef boost::variant<std::string, LLUUID, S32, ERlvBehaviour> RlvExceptionOption;
 
-typedef std::vector<LLViewerObject*> llvo_vec_t;
-typedef std::vector<const LLViewerObject*> c_llvo_vec_t;
+class RlvGCTimer;
+class RlvWLSnapshot;
 
 // ============================================================================
 // RlvSettings
@@ -177,6 +200,8 @@ bool rlvMenuEnableIfNot(const LLSD& sdParam);
 // Selection functors
 //
 
+bool rlvCanDeleteOrReturn();
+
 struct RlvSelectHasLockedAttach : public LLSelectedNodeFunctor
 {
 	RlvSelectHasLockedAttach() {}
@@ -234,12 +259,8 @@ protected:
 struct RlvPredIsEqualOrLinkedItem
 {
 	RlvPredIsEqualOrLinkedItem(const LLViewerInventoryItem* pItem) : m_pItem(pItem) {}
-	RlvPredIsEqualOrLinkedItem(const LLUUID& idItem) { m_pItem = gInventory.getItem(idItem); }
-
-	bool operator()(const LLViewerInventoryItem* pItem) const
-	{
-		return (m_pItem) && (pItem) && (m_pItem->getLinkedUUID() == pItem->getLinkedUUID());
-	}
+	RlvPredIsEqualOrLinkedItem(const LLUUID& idItem);
+	bool operator()(const LLViewerInventoryItem* pItem) const;
 protected:
 	const LLViewerInventoryItem* m_pItem;
 };
diff --git a/indra/newview/rlvextensions.cpp b/indra/newview/rlvextensions.cpp
index 68fa81f04b72ecd56526ba7055581edfd42bd552..d587c075f02c8ca28bbc8c5fd68659c47bdf2653 100644
--- a/indra/newview/rlvextensions.cpp
+++ b/indra/newview/rlvextensions.cpp
@@ -15,15 +15,15 @@
  */
 
 #include "llviewerprecompiledheaders.h"
-//#include "llagent.h"
+#include "llagent.h"
 #include "llagentcamera.h"
 #include "llviewercontrol.h"
-//#include "llviewerwindow.h"
-//#include "llvoavatar.h"
+#include "llvoavatarself.h"
 #include "llwlparammanager.h"
 
 #include "rlvextensions.h"
 #include "rlvhandler.h"
+#include "rlvhelper.h"
 
 // ============================================================================
 
diff --git a/indra/newview/rlvhandler.cpp b/indra/newview/rlvhandler.cpp
index c6b04f48233b4c8316141b115e6851bec8bb3429..710b72ef772a0d7bbf3cf881bc563ce73fc2a263 100644
--- a/indra/newview/rlvhandler.cpp
+++ b/indra/newview/rlvhandler.cpp
@@ -21,6 +21,7 @@
 #include "llcallbacklist.h"
 #include "llgroupactions.h"
 #include "llhudtext.h"
+#include "llstartup.h"
 #include "llviewermessage.h"
 #include "llviewerobjectlist.h"
 #include "llviewerparcelmgr.h"
@@ -106,6 +107,16 @@ bool RlvHandler::hasBehaviourRoot(const LLUUID& idObjRoot, ERlvBehaviour eBhvr,
 	return false;
 }
 
+// ============================================================================
+// Behaviour exception handling
+//
+
+// Checked: 2009-10-04 (RLVa-1.0.4a) | Modified: RLVa-1.0.4a
+void RlvHandler::addException(const LLUUID& idObj, ERlvBehaviour eBhvr, const RlvExceptionOption& varOption)
+{
+	m_Exceptions.insert(std::pair<ERlvBehaviour, RlvException>(eBhvr, RlvException(idObj, eBhvr, varOption)));
+}
+
 // Checked: 2009-10-04 (RLVa-1.0.4c) | Modified: RLVa-1.0.4c
 bool RlvHandler::isException(ERlvBehaviour eBhvr, const RlvExceptionOption& varOption, ERlvExceptionCheck typeCheck) const
 {
@@ -142,6 +153,28 @@ bool RlvHandler::isException(ERlvBehaviour eBhvr, const RlvExceptionOption& varO
 	return false;
 }
 
+// Checked: 2009-10-04 (RLVa-1.0.4a) | Modified: RLVa-1.0.4a
+bool RlvHandler::isPermissive(ERlvBehaviour eBhvr) const
+{
+	return (RlvCommand::hasStrictVariant(eBhvr)) 
+		? !((hasBehaviour(RLV_BHVR_PERMISSIVE)) || (isException(RLV_BHVR_PERMISSIVE, eBhvr, RLV_CHECK_PERMISSIVE)))
+		: true;
+}
+
+// Checked: 2009-10-04 (RLVa-1.0.4a) | Modified: RLVa-1.0.4a
+void RlvHandler::removeException(const LLUUID& idObj, ERlvBehaviour eBhvr, const RlvExceptionOption& varOption)
+{
+	for (rlv_exception_map_t::iterator itException = m_Exceptions.lower_bound(eBhvr), 
+			endException = m_Exceptions.upper_bound(eBhvr); itException != endException; ++itException)
+	{
+		if ( (itException->second.idObject == idObj) && (itException->second.varOption == varOption) )
+		{
+			m_Exceptions.erase(itException);
+			break;
+		}
+	}
+}
+
 // ============================================================================
 // Command processing functions
 //
@@ -308,6 +341,17 @@ ERlvCmdRet RlvHandler::processCommand(const RlvCommand& rlvCmd, bool fFromObj)
 	return eRet;
 }
 
+// Checked: 2009-11-25 (RLVa-1.1.0f) | Modified: RLVa-1.1.0f
+ERlvCmdRet RlvHandler::processCommand(const LLUUID& idObj, const std::string& strCommand, bool fFromObj)
+{
+	if (STATE_STARTED != LLStartUp::getStartupState())
+	{
+		m_Retained.push_back(RlvCommand(idObj, strCommand));
+		return RLV_RET_RETAINED;
+	}
+	return processCommand(RlvCommand(idObj, strCommand), fFromObj);
+}
+
 // Checked: 2010-02-27 (RLVa-1.2.0a) | Modified: RLVa-1.1.0f
 void RlvHandler::processRetainedCommands(ERlvBehaviour eBhvrFilter /*=RLV_BHVR_UNKNOWN*/, ERlvParamType eTypeFilter /*=RLV_TYPE_UNKNOWN*/)
 {
@@ -583,6 +627,33 @@ void RlvHandler::onTeleportFinished(const LLVector3d& posArrival)
 // String/chat censoring functions
 //
 
+// Checked: 2010-03-06 (RLVa-1.2.0c) | Added: RLVa-1.1.0j
+bool RlvHandler::canSit(LLViewerObject* pObj, const LLVector3& posOffset /*= LLVector3::zero*/) const
+{
+	// The user can sit on the specified object if:
+	//   - not prevented from sitting
+	//   - not prevented from standing up or not currently sitting
+	//   - not standtp restricted or not currently sitting (if the user is sitting and tried to sit elsewhere the tp would just kick in)
+	//   - [regular sit] not @sittp=n or @fartouch=n restricted or if they clicked on a point within 1.5m of the avie's current position
+	//   - [force sit] not @sittp=n restricted by a *different* object than the one that issued the command or the object is within 1.5m
+	return
+		( (pObj) && (LL_PCODE_VOLUME == pObj->getPCode()) ) &&
+		(!hasBehaviour(RLV_BHVR_SIT)) && 
+		( ((!hasBehaviour(RLV_BHVR_UNSIT)) && (!hasBehaviour(RLV_BHVR_STANDTP))) || 
+		  ((isAgentAvatarValid()) && (!gAgentAvatarp->isSitting())) ) &&
+		( ((NULL == getCurrentCommand() || (RLV_BHVR_SIT != getCurrentCommand()->getBehaviourType()))
+			? ((!hasBehaviour(RLV_BHVR_SITTP)) && (!hasBehaviour(RLV_BHVR_FARTOUCH)))	// [regular sit]
+			: (!hasBehaviourExcept(RLV_BHVR_SITTP, getCurrentObject()))) ||				// [force sit]
+		  (dist_vec_squared(gAgent.getPositionGlobal(), pObj->getPositionGlobal() + LLVector3d(posOffset)) < 1.5f * 1.5f) );
+}
+
+// Checked: 2010-03-07 (RLVa-1.2.0c) | Added: RLVa-1.2.0a
+bool RlvHandler::canStand() const
+{
+	// NOTE: return FALSE only if we're @unsit=n restricted and the avie is currently sitting on something and TRUE for everything else
+	return (!hasBehaviour(RLV_BHVR_UNSIT)) || ((isAgentAvatarValid()) && (!gAgentAvatarp->isSitting()));
+}
+
 // Checked: 2010-04-11 (RLVa-1.3.0h) | Modified: RLVa-1.3.0h
 bool RlvHandler::canTouch(const LLViewerObject* pObj, const LLVector3& posOffset /*=LLVector3::zero*/) const
 {
@@ -686,6 +757,12 @@ void RlvHandler::filterChat(std::string& strUTF8Text, bool fFilterEmote) const
 	}
 }
 
+// Checked: 2010-11-29 (RLVa-1.3.0c) | Added: RLVa-1.3.0c
+bool RlvHandler::hasException(ERlvBehaviour eBhvr) const
+{
+	return (m_Exceptions.find(eBhvr) != m_Exceptions.end());
+}
+
 // Checked: 2010-02-27 (RLVa-1.2.0b) | Modified: RLVa-1.2.0a
 bool RlvHandler::redirectChatOrEmote(const std::string& strUTF8Text) const
 {
diff --git a/indra/newview/rlvhandler.h b/indra/newview/rlvhandler.h
index 320cdaf9acb77fee45e92f000fcfe5c67c1c3acc..209ad17a5818b523a6e4f68ac3f4e8bbd008b563 100644
--- a/indra/newview/rlvhandler.h
+++ b/indra/newview/rlvhandler.h
@@ -18,14 +18,8 @@
 #define RLV_HANDLER_H
 
 #include <stack>
-#include "llagentconstants.h"
-#include "llstartup.h"
-#include "llviewerjointattachment.h"
-#include "llviewerobject.h"
 
 #include "rlvcommon.h"
-#include "rlvhelper.h"
-#include "rlvlocks.h"
 
 // ============================================================================
 
@@ -235,12 +229,6 @@ extern rlv_handler_t gRlvHandler;
 // Inlined member functions
 //
 
-// Checked: 2009-10-04 (RLVa-1.0.4a) | Modified: RLVa-1.0.4a
-inline void RlvHandler::addException(const LLUUID& idObj, ERlvBehaviour eBhvr, const RlvExceptionOption& varOption)
-{
-	m_Exceptions.insert(std::pair<ERlvBehaviour, RlvException>(eBhvr, RlvException(idObj, eBhvr, varOption)));
-}
-
 // Checked: 2010-11-29 (RLVa-1.3.0c) | Added: RLVa-1.3.0c
 inline bool RlvHandler::canEdit(const LLViewerObject* pObj) const
 {
@@ -285,26 +273,6 @@ inline bool RlvHandler::canShowHoverText(const LLViewerObject *pObj) const
 			   (isException(RLV_BHVR_SHOWHOVERTEXT, pObj->getID(), RLV_CHECK_PERMISSIVE)) ) );
 }
 
-// Checked: 2010-03-06 (RLVa-1.2.0c) | Added: RLVa-1.1.0j
-inline bool RlvHandler::canSit(LLViewerObject* pObj, const LLVector3& posOffset /*= LLVector3::zero*/) const
-{
-	// The user can sit on the specified object if:
-	//   - not prevented from sitting
-	//   - not prevented from standing up or not currently sitting
-	//   - not standtp restricted or not currently sitting (if the user is sitting and tried to sit elsewhere the tp would just kick in)
-	//   - [regular sit] not @sittp=n or @fartouch=n restricted or if they clicked on a point within 1.5m of the avie's current position
-	//   - [force sit] not @sittp=n restricted by a *different* object than the one that issued the command or the object is within 1.5m
-	return
-		( (pObj) && (LL_PCODE_VOLUME == pObj->getPCode()) ) &&
-		(!hasBehaviour(RLV_BHVR_SIT)) && 
-		( ((!hasBehaviour(RLV_BHVR_UNSIT)) && (!hasBehaviour(RLV_BHVR_STANDTP))) || 
-		  ((isAgentAvatarValid()) && (!gAgentAvatarp->isSitting())) ) &&
-		( ((NULL == getCurrentCommand() || (RLV_BHVR_SIT != getCurrentCommand()->getBehaviourType()))
-			? ((!hasBehaviour(RLV_BHVR_SITTP)) && (!hasBehaviour(RLV_BHVR_FARTOUCH)))	// [regular sit]
-			: (!hasBehaviourExcept(RLV_BHVR_SITTP, getCurrentObject()))) ||				// [force sit]
-		  (dist_vec_squared(gAgent.getPositionGlobal(), pObj->getPositionGlobal() + LLVector3d(posOffset)) < 1.5f * 1.5f) );
-}
-
 inline bool RlvHandler::canStartIM(const LLUUID& idRecipient) const
 {
 	// User can start an IM session with "recipient" (could be an agent or a group) if:
@@ -315,13 +283,6 @@ inline bool RlvHandler::canStartIM(const LLUUID& idRecipient) const
 		( (!hasBehaviour(RLV_BHVR_STARTIMTO)) || (!isException(RLV_BHVR_STARTIMTO, idRecipient)) );
 }
 
-// Checked: 2010-03-07 (RLVa-1.2.0c) | Added: RLVa-1.2.0a
-inline bool RlvHandler::canStand() const
-{
-	// NOTE: return FALSE only if we're @unsit=n restricted and the avie is currently sitting on something and TRUE for everything else
-	return (!hasBehaviour(RLV_BHVR_UNSIT)) || ((isAgentAvatarValid()) && (!gAgentAvatarp->isSitting()));
-}
-
 // Checked: 2010-12-11 (RLVa-1.2.2c) | Added: RLVa-1.2.2c
 inline bool RlvHandler::canTeleportViaLure(const LLUUID& idAgent) const
 {
@@ -338,44 +299,6 @@ inline bool RlvHandler::hasBehaviourExcept(ERlvBehaviour eBhvr, const LLUUID& id
 	return hasBehaviourExcept(eBhvr, LLStringUtil::null, idObj);
 }
 
-// Checked: 2010-11-29 (RLVa-1.3.0c) | Added: RLVa-1.3.0c
-inline bool RlvHandler::hasException(ERlvBehaviour eBhvr) const
-{
-	return (m_Exceptions.find(eBhvr) != m_Exceptions.end());
-}
-
-inline bool RlvHandler::isPermissive(ERlvBehaviour eBhvr) const
-{
-	return (RlvCommand::hasStrictVariant(eBhvr)) 
-		? !((hasBehaviour(RLV_BHVR_PERMISSIVE)) || (isException(RLV_BHVR_PERMISSIVE, eBhvr, RLV_CHECK_PERMISSIVE)))
-		: true;
-}
-
-// Checked: 2009-10-04 (RLVa-1.0.4a) | Modified: RLVa-1.0.4a
-inline void RlvHandler::removeException(const LLUUID& idObj, ERlvBehaviour eBhvr, const RlvExceptionOption& varOption)
-{
-	for (rlv_exception_map_t::iterator itException = m_Exceptions.lower_bound(eBhvr), 
-			endException = m_Exceptions.upper_bound(eBhvr); itException != endException; ++itException)
-	{
-		if ( (itException->second.idObject == idObj) && (itException->second.varOption == varOption) )
-		{
-			m_Exceptions.erase(itException);
-			break;
-		}
-	}
-}
-
-// Checked: 2009-11-25 (RLVa-1.1.0f) | Modified: RLVa-1.1.0f
-inline ERlvCmdRet RlvHandler::processCommand(const LLUUID& idObj, const std::string& strCommand, bool fFromObj)
-{
-	if (STATE_STARTED != LLStartUp::getStartupState())
-	{
-		m_Retained.push_back(RlvCommand(idObj, strCommand));
-		return RLV_RET_RETAINED;
-	}
-	return processCommand(RlvCommand(idObj, strCommand), fFromObj);
-}
-
 // ============================================================================
 
 #endif // RLV_HANDLER_H
diff --git a/indra/newview/rlvhelper.cpp b/indra/newview/rlvhelper.cpp
index 60a5260674190ccc2fad7d0a2aece18c5afa8623..8382b20618a91c8c05c68882448c55e3c4934c86 100644
--- a/indra/newview/rlvhelper.cpp
+++ b/indra/newview/rlvhelper.cpp
@@ -1087,32 +1087,6 @@ BOOL RlvGCTimer::tick()
 // Various helper functions
 //
 
-// Checked: 2010-04-11 (RLVa-1.2.0b) | Modified: RLVa-0.2.0g
-bool rlvCanDeleteOrReturn()
-{
-	bool fIsAllowed = true;
-
-	if (gRlvHandler.hasBehaviour(RLV_BHVR_REZ))
-	{
-		// We'll allow if none of the prims are owned by the avie or group owned
-		LLObjectSelectionHandle handleSel = LLSelectMgr::getInstance()->getSelection();
-		RlvSelectIsOwnedByOrGroupOwned f(gAgent.getID());
-		if ( (handleSel.notNull()) && ((0 == handleSel->getRootObjectCount()) || (NULL != handleSel->getFirstRootNode(&f, FALSE))) )
-			fIsAllowed = false;
-	}
-	
-	if ( (gRlvHandler.hasBehaviour(RLV_BHVR_UNSIT)) && (isAgentAvatarValid()) )
-	{
-		// We'll allow if the avie isn't sitting on any of the selected objects
-		LLObjectSelectionHandle handleSel = LLSelectMgr::getInstance()->getSelection();
-		RlvSelectIsSittingOn f(gAgentAvatarp->getRoot());
-		if ( (handleSel.notNull()) && (handleSel->getFirstRootNode(&f, TRUE)) )
-			fIsAllowed = false;
-	}
-
-	return fIsAllowed;
-}
-
 // ============================================================================
 // Attachment group helper functions
 //
diff --git a/indra/newview/rlvhelper.h b/indra/newview/rlvhelper.h
index ee6bea9a8b798da362b21a736ca67cce18b4c745..45cb50cf68a387b3524e59e7b9251379cad3a470 100644
--- a/indra/newview/rlvhelper.h
+++ b/indra/newview/rlvhelper.h
@@ -18,20 +18,13 @@
 #define RLV_HELPER_H
 
 #include "lleventtimer.h"
+#include "llinventorymodel.h"
+#include "llviewerinventory.h"
 #include "llwlparamset.h"
 
 #include "rlvdefines.h"
 #include "rlvcommon.h"
 
-#ifdef LL_WINDOWS
-	#pragma warning (push)
-	#pragma warning (disable : 4702) // warning C4702: unreachable code
-#endif
-#include <boost/variant.hpp>
-#ifdef LL_WINDOWS
-	#pragma warning (pop)
-#endif
-
 // ============================================================================
 // RlvCommand
 //
@@ -88,7 +81,6 @@ protected:
 
 	friend class RlvHandler;
 };
-typedef std::list<RlvCommand> rlv_command_list_t;
 
 // ============================================================================
 // RlvCommandOption (and derived classed)
@@ -358,8 +350,6 @@ protected:
 // RlvException
 //
 
-typedef boost::variant<std::string, LLUUID, S32, ERlvBehaviour> RlvExceptionOption;
-
 struct RlvException
 {
 public:
@@ -376,7 +366,7 @@ private:
 // RlvWLSnapshot
 //
 
-struct RlvWLSnapshot
+class RlvWLSnapshot
 {
 public:
 	static void           restoreSnapshot(const RlvWLSnapshot* pWLSnapshot);
@@ -425,8 +415,6 @@ inline void rlvCallbackTimerOnce(F32 nPeriod, RlvCallbackTimerOnce::nullary_func
 // Various helper functions
 //
 
-bool rlvCanDeleteOrReturn();
-
 ERlvAttachGroupType rlvAttachGroupFromIndex(S32 idxGroup);
 ERlvAttachGroupType rlvAttachGroupFromString(const std::string& strGroup);
 
diff --git a/indra/newview/rlvlocks.cpp b/indra/newview/rlvlocks.cpp
index 0ca9105071ab6561c69326a9c5e20a7ee816d31d..b2414d438e9abba1d2f2913a761d57c4b3cd42a0 100644
--- a/indra/newview/rlvlocks.cpp
+++ b/indra/newview/rlvlocks.cpp
@@ -511,7 +511,7 @@ void RlvAttachmentLockWatchdog::detach(S32 idxAttachPt, const LLViewerObject* pA
 	if (!pAttachPt)
 		return;
 
-	c_llvo_vec_t attachObjs;
+	std::vector<const LLViewerObject*> attachObjs;
 	for (LLViewerJointAttachment::attachedobjs_vec_t::const_iterator itAttachObj = pAttachPt->mAttachedObjects.begin();
 			itAttachObj != pAttachPt->mAttachedObjects.end(); ++itAttachObj)
 	{
@@ -527,7 +527,7 @@ void RlvAttachmentLockWatchdog::detach(S32 idxAttachPt, const LLViewerObject* pA
 		gMessageSystem->addUUIDFast(_PREHASH_AgentID, gAgent.getID());
 		gMessageSystem->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
 		
-		for (c_llvo_vec_t::const_iterator itAttachObj = attachObjs.begin(); itAttachObj != attachObjs.end(); ++itAttachObj)
+		for (std::vector<const LLViewerObject*>::const_iterator itAttachObj = attachObjs.begin(); itAttachObj != attachObjs.end(); ++itAttachObj)
 		{
 			const LLViewerObject* pAttachObj = *itAttachObj;
 			gMessageSystem->nextBlockFast(_PREHASH_ObjectData);
@@ -608,7 +608,6 @@ void RlvAttachmentLockWatchdog::onAttach(const LLViewerObject* pAttachObj, const
 				else
 				{
 					// Iterate over all the current attachments and force detach any that shouldn't be there
-					c_llvo_vec_t attachObjs;
 					for (LLViewerJointAttachment::attachedobjs_vec_t::const_iterator itAttachObj = pAttachPt->mAttachedObjects.begin();
 							itAttachObj != pAttachPt->mAttachedObjects.end(); ++itAttachObj)
 					{
diff --git a/indra/newview/rlvlocks.h b/indra/newview/rlvlocks.h
index 31cfb0c1fa192d6ef6f9109f7a773ee4f3a68044..c0a5fed89f34b06d5abb124e255e848931d73cac 100644
--- a/indra/newview/rlvlocks.h
+++ b/indra/newview/rlvlocks.h
@@ -23,15 +23,6 @@
 #include "rlvdefines.h"
 #include "rlvcommon.h"
 
-#ifdef LL_WINDOWS
-	#pragma warning (push)
-	#pragma warning (disable : 4702) // warning C4702: unreachable code
-#endif
-#include <boost/variant.hpp>
-#ifdef LL_WINDOWS
-	#pragma warning (pop)
-#endif
-
 // ============================================================================
 // RlvAttachPtLookup class declaration
 //
diff --git a/indra/newview/rlvui.cpp b/indra/newview/rlvui.cpp
index 0c7631e1d065d3474f25a6e88852767d33482857..37b7d3c8be09ce47a1a3cefe9f0a0681daa5e361 100644
--- a/indra/newview/rlvui.cpp
+++ b/indra/newview/rlvui.cpp
@@ -15,6 +15,7 @@
  */
 
 #include "llviewerprecompiledheaders.h"
+#include "llagent.h"
 #include "llavatarlist.h"				// Avatar list control used by the "Nearby" tab in the "People" sidebar panel
 #include "llavatarnamecache.h"
 #include "llbottomtray.h"
@@ -40,6 +41,7 @@
 #include "llteleporthistorystorage.h"
 #include "lltoolmgr.h"
 #include "llviewerparcelmgr.h"
+#include "llvoavatar.h"
 #include "roles_constants.h"			// Group "powers"
 
 #include "rlvui.h"