Optimize this code, improve the code, structure the code, improve the interface in this lua code for teardown
```lua

function DrawDynamicTextButtons(condition, text1, text2, w, h, font, size, R, G, B)
    local press = false
    UiPush()
        UiButtonPressDist(3)
        UiFont(font, size)
        UiButtonImageBox("ui/common/box-outline-6.png", 6, 6)
        if condition then
            UiColor(R, G, B)
            UiButtonHoverColor(R*10, G*0.6, B*0.2)
            if UiTextButton(text2, w, h) then
                condition = not condition
                press = true
            end
        else
            UiColor(1, 1, 1)
            UiButtonHoverColor(R*0.6, G, B*0.6)
            if UiTextButton(text1, w, h) then
                condition = not condition
                press = true
            end
        end
    UiPop()
    return press, condition
end

-- need to copy and paste this because the #include keyword stopped working
function KeyInit()
	setKey("savegame.mod.fire", true, true)
	setKey("savegame.mod.dca", true, true)
	setKey("savegame.mod.damage", true, true)
	setKey("savegame.mod.machinegun", true, true)
	setKey("savegame.mod.aircraft", true, true)
	setKey("savegame.mod.mortar", true, true)
	setKey("savegame.mod.music", true, true)
	setKey("savegame.mod.lowconfig", false, true)
	setKey("savegame.mod.start", false, true)
	setKey("savegame.mod.start.key", 'c', false)
	setKey("savegame.mod.flak", true, true)
end

function setKey(key, default, boolean, infinite)

	boolean = boolean or false
	infinite = infinite or false
	
	if not HasKey(key) then
		if boolean then
			SetBool(key, default)
		else
			SetString(key, default)
		end
	end
	if infinite then
		if not HasKey(key .. "_infinite") then
			SetBool(key .. "_infinite", false)
		end
	end
end

function init()
	KeyInit()
end


function draw()
	UiTranslate(UiCenter(), 0)
	UiAlign("center middle")

	--Draw buttons
	UiTranslate(0, 0)
	UiFont("regular.ttf", 26)
	UiButtonImageBox("ui/common/box-outline-6.png", 6, 6)
	
	local border = 80
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.fire", "Toggle this to randomnly spawn fire on start.")
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.dca", "Toggle this to have some DCA shots in the sky.")
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.damage", "Toggle this to randomnly destroy parts of the map on start (laggy during the first 30s of the simulation, as it progressively destroys the map).")
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.lowconfig", "Toggle this if you have a small config and the previous feature make you crash or lag for too long.")
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.machinegun", "Toggle this to have machine gun sounds around you.")
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.aircraft", "Toggle this to make an aircraft pass sometimes in the sky.")
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.mortar", "Toggle this to add random mortar shots on the map.")
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.music", "Toggle this to activate the music.")
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.start", "Toggle this to activate the mod when you press any key you want instead of on-start. This allows you to freely set up your scene and also ending it.")
	UiTranslate(0, border)
	triggerkey = GetString("savegame.mod.start.key")
	if triggerkey ~= "" then
		keyDisplay = triggerkey
	else
		keyDisplay = "Not Assigned"
	end
	change, changeKey = DrawDynamicTextButtons(changeKey, keyDisplay, "Press A Key", 150, 35, "regular.ttf", 26, 1, 1, 1)
	if changeKey and not autoEQ then
		if InputLastPressedKey() == "tab" or InputLastPressedKey() == "esc" then
			changeKey = false
		else
			if InputPressed(",") then
				changeKey = false
				SetString("savegame.mod.start.key", ",")
			elseif InputPressed(".") then
				changeKey = false
				SetString("savegame.mod.start.key", ".")
			elseif InputLastPressedKey() ~= "" then
				changeKey = false
				SetString("savegame.mod.start.key", InputLastPressedKey())
			end
		end
	else
		changeKey = false
	end
	
	UiTranslate(0, border)
	DrawBoolButton("savegame.mod.flak", "Toggle this to activate the flak explosions in the sky.")
	
	UiTranslate(0, 100)
	if UiTextButton("Close", 200, 40) then
		Menu()
	end
end

local function bool_to_int(value)
	return value == true and 1 or value == false and 0
end

local function int_to_bool(value)
	return value > 0
end

function DrawIntSlider(key, lowerbound, upperbound, size, text, infinite)
	infinite = infinite or false
	UiPush()
		UiAlign("center middle")
		UiFont("regular.ttf", 22)
		UiColor(0.9,0.9,0.025,1)
		local value = GetInt(key)
		if GetBool(key .. "_infinite") then
			value = "Infinite"
		end
		UiText(text .. " " .. value, true)
		UiColor(1,1,1,1)
		UiFont("regular.ttf", 26)
		UiImageBox("box-solid-6.png", (upperbound * size) - (lowerbound * (size * 0.5)), 20, 5, 5)
		UiTranslate(-((upperbound * (size*0.5)) + (lowerbound * (size*0.5))), 0)
		SetInt(key, UiSlider("dot_small.png", "x", GetInt(key) * size, lowerbound * size, upperbound * size) / size)
		if infinite then
			SetBool(key .. "_infinite", GetInt(key) == upperbound)
		end
	UiPop()
end

function DrawBoolButton(key, text)
	UiPush()
		UiAlign("center middle")
		UiFont("regular.ttf", 22)
		UiColor(0.9,0.9,0.025,1)
		UiText(text, true)
		UiTranslate(0, 15)
		if GetBool(key) then
			UiColor(0,1,0,1)
		else
			UiColor(1,0,0,1)
		end
		UiFont("regular.ttf", 26)
		if UiTextButton("Toggled: " .. tostring(GetBool(key))) then
			SetBool(key, not GetBool(key))
		end
	UiPop()
end
```