Added Anton's fixes from other branch.