Refactored the fix for #3222.