Updated error handling to use perror