improved impl of #624