Refactors and fixes fb1d115262 regression.