Backport of 152d0d92ff9bd79608d57369ec4c5bfe746e1919 made in master branch