Revert r222416, r222422, r222426: the former revision had problems and fixing them introduced bugs