backport important fixes from dev branch