you can just reuse $path as of line 64 above - no need to call dirname