See merge request !3968 (merged) and !4076 (merged)
(cherry picked from commit f2425eb7 and 0c8035a5)