Updated code with a second pattern' (more robust for .extensions)