Fix bug #2732 and #2609 (Paulo Dias commit).