Added patch #5850 and #5851 from Nelson