Applying patch from gregbell#1896