         clear = toolbar.appendChild(document.createElement('div'))
         clear.classList.add('clear')
 
-        i = 0
-        for resultset in resultsets
+        for resultset, i in resultsets
             @element.appendChild(@createTable(resultset, i))
-            i++
 
     destroy: ->
         @element.remove()

    resultsets: []

    canTypeBeSorted: (typeCode) ->
        t = Type[typeCode]
        if t
            return t.compare?

        return false

    compare: (typeCode, left, right, asc) ->
        type = Type[typeCode]

        if type.compare?
            val = type.compare(left, right)

            if !asc
                val = val * -1

            return val

        return 0

    sort: (resultset, columnIndex, asc, resultsView) ->
        if resultset.columns[columnIndex].ascending?
            resultset.columns[columnIndex].ascending = !resultset.columns[columnIndex].ascending
        else

            console.error('This type is not sortable', typeCode)
            return

        resultset.rows.sort (left, right) =>
            return @compare(typeCode, left[columnIndex], right[columnIndex], ascending)

    createTh: (text, resultsetIndex, columnIndex) ->
        th = document.createElement('th')
        th.textContent = text
        th.setAttribute('column_index', columnIndex)
        th.setAttribute('resultset_index', resultsetIndex)
        if @canTypeBeSorted(@resultsets[resultsetIndex].columns[columnIndex].type_code)
            th.classList.add('sortable')
            if @resultsets[resultsetIndex].columns[columnIndex].ascending == true
                th.textContent = th.textContent + ' +'
            else if @resultsets[resultsetIndex].columns[columnIndex].ascending == false
                th.textContent = th.textContent + ' -'

        th.onclick = =>
            @sort(@resultsets[this.getAttribute('resultset_index')], this.getAttribute('column_index'), true)
            @update(@resultsets)
        return th

    createTable: (x, resultsetIndex) ->
