Improve pem file error checking