Improved extension detection similar to 27ea81a4 and 4df61444.