First cut of #534 with shameless copypasta