added stat to check file exists before attempting to open