Cherry-pick relevant changes from #291 to master