Rebased #3965

Rebased version of @j00seph's PR: #3965