Added a better fix for ticket #13037 to the 2.1.X branch, previous fix was done in master branch
Cherry-picked 7a48d5 and 10f031 from 2.1.x branch to master