applying patch from upstream svn commits r1724 and r1725