CherryPicked from @zapu commit d7b8787cab9b670ed6b5e47ed4a1eb871e298a6e