KeyError bugfix backported from devel.
Infinite loop bugfix backported from devel.