Better fix for bug #3349 (almost all the needed code was there :-)