do
	local originalCreateUI = CreateUI
	local originalOnSelectionChanged = OnSelectionChanged
	local originalOnBeat = OnBeat

	-- Get options
	local Prefs = import('/lua/user/prefs.lua')
	local options = Prefs.GetFromCurrentProfile('options')

	function CreateUI(isReplay)
		originalCreateUI(isReplay)
		import("/mods/W_Groups/modules/groupinterface.lua").Init()
	end

	-- Dummy variable. If this is not set, let's now waste CPU cycles
	local doAutomaticSelection = false
	local labelToSelect = ""

	function OnSelectionChanged(oldSelection, newSelection, added, removed)
		-- If we have a current Squad, update the current squad indicator to reflect the new selection      
		if import("/mods/W_Groups/modules/groupinterface.lua").CurrSquad then
			local currLabel = import("/mods/W_groups/modules/groupinterface.lua").GetCurrentLabel()
			if (table.getn(newSelection) > 0) then
				for i, newUnit in newSelection do
					local selectionsetshere = newUnit:GetSelectionSets()
					if newUnit:HasSelectionSet(currLabel) == false then
						-- We're obviously selecting something outside the current squad, reset the indicator
						import("/mods/W_Groups/modules/groupinterface.lua").SetCurrSquad(false)
						break
					end
				end
			else
				-- Nothing selected, reset indicator
				import("/mods/W_Groups/modules/groupinterface.lua").SetCurrSquad(false)
			end    
		else
--			if ((IsKeyDown(18) or import("/mods/W_Groups/modules/groupinterface.lua").autoselectinverse) and (not (IsKeyDown(18) and import("/mods/W_Groups/modules/groupinterface.lua").autoselectinverse))) then
--				print('True')
--			else
--				print('False')
--			end
			-- Only fire this on mouse selection where it is certain that currsquad is false (fucking xor)
			if(table.getn(newSelection) == 1) and ((IsKeyDown(18) or import("/mods/W_Groups/modules/groupinterface.lua").autoselectinverse) and not (IsKeyDown(18) and import("/mods/W_Groups/modules/groupinterface.lua").autoselectinverse)) then
				doAutomaticSelection = true
				local selectiontemp = newSelection[1]:GetSelectionSets()
				labelToSelect = selectiontemp[1]
			end
		end
		-- Run old selection code
		originalOnSelectionChanged(oldSelection, newSelection, added, removed)
	end

	-- let's have some keybindings
	import('/mods/W_Groups/modules/keymapping.lua').Init()

	-- Run the autoselection stuff, but to save cycles, don't even do this functionality insert if we aren't using the feature
	-- Yes, if you change settings, RESTART
	if(options.gui_autoselect ~= 0) then
		function OnBeat()
			if (doAutomaticSelection == true) then
				import("/mods/W_Groups/modules/groupinterface.lua").AutomaticSelection(labelToSelect)
				doAutomaticSelection = false
			end
			originalOnBeat()
		end
	end
end
