fixed malloc and closedir errors