New version that allows to override dirname