Update with hayd's logic [Fix #5505]