Fix #2490 (an improved version of PR #2534)