Revert "Revert "Reapply from master branch (4234340323b55fcc90137e3d8292426b7a6e834a)""

This reverts commit 1c1bcfcb930da67bd3b9581ea4a2f610c9eb0438.