added more robust file opening method