MODE-2271-3.x Ported & adapted the code from MODE-2271 to the 3.x branch