Cherry picking header code from 2.2 branch

@spiralman When you have copious amounts of spare time. :)