Backported changes and fixes from master

Change-Id: Iebfdd81513c193d95b8354553ef8a2986b338d6c