Moved part of fixes from another branch