Port more API fixes from Petr's branch.