Changed fopen/fclose to low-level open/close