Backport 3946 to stable5

Backport of #3946 in stable5

Some quick reviews please
@schiesbn @karlitschek @butonic @icewind1991 @MTGap THX