Manually applied patch from KC511 branch