fixing bug #5418 by integrating Miguel's fix manually