if XPCount == nil then
    d("Initializing LRT")
    XPCount = {}
    XPCount.curZone = ""
    XPCount.zoneStart = 0
    XPCount.zoneXpStart = 0
    XPCount.zoneVpStart = 0
    XPCount.clockStart = 0
    XPCount.clockXpStart = 0
    XPCount.clockVpStart = 0
    XPCount.init = function(_, _)
        EVENT_MANAGER:UnregisterForEvent("XPCount", EVENT_PLAYER_ACTIVATED)
        XPCount.set()
        XPCount.zoneSet()
        EVENT_MANAGER:RegisterForEvent("XPCountPlayerActivate", EVENT_PLAYER_ACTIVATED, XPCount.zoneChange)
        EVENT_MANAGER:RegisterForEvent("XPCountZoneChange", EVENT_ZONE_CHANGED, XPCount.zoneChange)
    end
    EVENT_MANAGER:RegisterForEvent("XPCount", EVENT_PLAYER_ACTIVATED, XPCount.init)
else
    d("Should show info here...")
end

XPCount.zoneSet = function(_, _, _, _)
    XPCount.curZone = GetUnitZone("player")
    XPCount.clockStart = GetTimeStamp()
    XPCount.clockXpStart = GetUnitXP("player")
    XPCount.clockVpStart = GetUnitVeteranPoints("player")
end

XPCount.set = function(_, _, _, _)
    XPCount.curZone = GetUnitZone("player")
    XPCount.zoneStart = GetTimeStamp()
    XPCount.zoneXpStart = GetUnitXP("player")
    XPCount.zoneVpStart = GetUnitVeteranPoints("player")
end

XPCount.zoneChange = function()
    local playerZone = GetUnitZone("player")
    if playerZone ~= XPCount.curZone then
        if XPCount.curZone ~= "" then
            d("Exiting " .. XPCount.curZone .. " into " .. playerZone .. ":")
            XPCount.showStats()
        end

        XPCount.zoneSet()
    end
end

XPCount.formatTime = function(delta)
    local sec = delta % 60
    delta = (delta - sec) / 60
    local min = delta % 60
    local hrs = (delta - min) / 60

    local out = ""
    if hrs > 0 then
        out = out .. hrs .. "h "
    end
    out = out .. min .. "m " .. sec .. "s"
    return out
end

XPCount.showStats = function()
    local time = GetTimeStamp()
    local curXP = GetUnitXP("player")
    local curVP = GetUnitVeteranPoints("player")
    local xp = curXP - XPCount.zoneXpStart
    local vp = curVP - XPCount.zoneVpStart
    local elapsed = GetDiffBetweenTimeStamps(time, XPCount.zoneStart)
    d("Time in zone: " .. XPCount.formatTime(elapsed))
    if vp > 0 then
        d("Experience earned: "  .. vp .. "vp")
    elseif xp > 0 then
        d("Experience earned: " .. xp .. "xp")
    end
    if elapsed > 0 then
        if vp > 0 then
            d("Rate: " .. math.floor((vp/elapsed)*3600) .. "vp/h")
        elseif xp > 0 then
            d("Rate: " .. math.floor((xp/elapsed)*3600) .. "xp/h")
        end
    end
end

SLASH_COMMANDS["/xpcount"] = function(arg)
    if arg == set then
        XPCount.curZone = GetUnitZone("player")
        d("Zone set to: " .. XPCount.curZone)
    else
        XPCount.showStats()
    end
end