Updated version of https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11918/commits#note_31616373