Directly copied from branch 1.6.4.