Port a couple of fixes from master branch