Merge #3589 with tests

Fix #3589