restore some code I removed in r85788, refactor it into