(function (global, factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module unless amdModuleId is set
    define([], function () {
      return (global['lrStickyHeader'] = factory(global));
    });
  } else if (typeof exports === 'object') {
    // Node. Does not work with strict CommonJS, but
    // only CommonJS-like environments that support module.exports,
    // like Node.
    module.exports = factory(global);
  } else {
    global['lrStickyHeader'] = factory(global);
  }
})(window, function factory (window) {
  'use strict';
  function getOffset (element, property) {
    var offset = element[property];
    var parent = element;
    while ((parent = parent.offsetParent) !== null) {
      offset += parent[property];
    }
    return offset;
  }

  var sticky = {
    //todo some memoize stuff
    setWidth: function setWidth () {
      var firstRow = this.tbody.getElementsByTagName('TR')[0];
      var trh = this.thead.getElementsByTagName('TR')[0];
      var firstTds;
      var firstThs;

      function setCellWidth (cell) {
        cell.style.width = cell.offsetWidth + 'px';
      }

      if (firstRow && trh) {
        firstTds = firstRow.getElementsByTagName('TD');
        firstThs = trh.getElementsByTagName('TH');

        [].forEach.call(firstTds, setCellWidth);
        [].forEach.call(firstThs, setCellWidth);
      }
    },
    eventListener: function eventListener () {
      var offsetTop = getOffset(this.thead, 'offsetTop');
      var offsetLeft = getOffset(this.thead, 'offsetLeft');
      var classes = this.thead.className.split(' ');

      if (this.stick !== true) {
        if (offsetTop - window.scrollY < 0) {
          this.stick = true;
          this.treshold = offsetTop;
          this.setWidth();
          this.thead.style.left = offsetLeft + 'px';
          this.thead.style.top = 0;
          setTimeout(function () {
            classes.push('lr-sticky-header');
            this.thead.style.position = 'fixed';
            this.thead.className = classes.join(' ');
          }.bind(this), 0)
        }
      }

      if (this.stick === true) {
        if (this.treshold - window.scrollY > 0) {
          this.stick = false;
          this.thead.style.position = 'initial';
          classes.splice(classes.indexOf('lr-sticky-header'), 1);
          this.thead.className = (classes).join(' ');
        }
      }
    }
  };

  return function lrStickyHeader (tableElement) {
    var thead;
    var tbody;

    if (tableElement.tagName !== 'TABLE') {
      throw new Error('lrStickyHeader only works on table element');
    }

    tbody = tableElement.getElementsByTagName('TBODY');
    thead = tableElement.getElementsByTagName('THEAD');
    if (!thead.length) {
      throw new Error('could not find the thead group element');
    }

    if (!tbody.length) {
      throw new Error('could not find the tbody group element');
    }

    thead = thead[0];
    tbody = tbody[0];


    var stickyTable = Object.create(sticky, {
      element: {value: tableElement},
      thead: {
        get: function () {
          return thead;
        }
      },
      tbody: {
        get: function () {
          return tbody;
        }
      }
    });

    var listener = stickyTable.eventListener.bind(stickyTable);
    window.addEventListener('scroll', listener);
    stickyTable.clean = function clean () {
      window.removeEventListener('scroll', listener);
    };

    return stickyTable;
  };

});


@@ -1645,30 +1645,19 @@
 !== true
-) %7B%0A        if
+ &&
  (offset
@@ -1681,16 +1681,17 @@
 llY %3C 0)
+)
  %7B%0A     
@@ -1685,34 +1685,32 @@
 %3C 0)) %7B%0A        
-  
 this.stick = tru
@@ -1712,18 +1712,16 @@
 = true;%0A
-  
         
@@ -1751,26 +1751,24 @@
 op;%0A        
- 
- 
 this.setWidt
@@ -1772,18 +1772,16 @@
 idth();%0A
-  
         
@@ -1819,26 +1819,24 @@
 eft + 'px';%0A
-  
         this
@@ -1865,18 +1865,16 @@
         
-  
 setTimeo
@@ -1882,34 +1882,32 @@
 t(function () %7B%0A
-  
           classe
@@ -1936,34 +1936,32 @@
 er');%0A          
- 
- 
 this.thead.style
@@ -1981,18 +1981,16 @@
 fixed';%0A
-  
         
@@ -2029,26 +2029,24 @@
 .join(' ');%0A
-  
         %7D.bi
@@ -2057,26 +2057,17 @@
 his), 0)
-%0A        %7D
+;
 %0A      %7D
@@ -2101,22 +2101,11 @@
 true
-) %7B%0A        if
+ &&
  (th
@@ -2137,21 +2137,20 @@
 llY %3E 0)
+)
  %7B%0A
-  
         
@@ -2169,34 +2169,32 @@
  false;%0A        
- 
- 
 this.thead.style
@@ -2216,18 +2216,16 @@
 itial';%0A
-  
         
@@ -2280,18 +2280,16 @@
 '), 1);%0A
-  
         
@@ -2332,26 +2332,16 @@
 n(' ');%0A
-        %7D%0A
       %7D%0A
@@ -2548,25 +2548,24 @@
 nt');%0A    %7D%0A
-%0A
     tbody = 
@@ -2660,24 +2660,25 @@
 e('THEAD');%0A
+%0A
     if (!the
@@ -3415,14 +3415,13 @@
 e;%0A  %7D;%0A
-%0A
 %7D);%0A%0A

refactor conditions