Cleanup and fix keychain related code.