Replace some perror with appropriate function that raises exception