Slightly rework @dmatushkin's PR.