This is logic from #431