Refactor heuristics (updated)

Same as https://github.com/github/linguist/pull/1784, but against master.