Cherry pick pr296

This just cherry-picks some commits from #296