Fixes #5115 Fixes couple issues in the logic which should finally solve reported issues.
Merge remote-tracking branch 'origin/master'
Merge remote-tracking branch 'origin/master'