Refactored generate_certificates