Port a bugfix from DesertBot