Fix error handling
Do not depend on d_type, use stat