API keys now stored in encrypted state, refactoring, bugfix