Refactor API key handling.