Refactor key handling.