Rewrote table header generation

This fixes sort index issue