better handle of opening files