Remove unused imports and shadowing names.
Simplify error handling for missing key codes.