cleanup cert validation

Organize tasks from smaller to larger ones, and extract the hackish part of interacting with the environment.