Re-rolling #22 patch against 2.x branch.