Fix some extra frees (dirname and basename don't allocate anything). Fix behavior with some paths. Free stuff that should be freed.