optimized basename method