added better error checking for fopen