     public static var authUrl        = "https://soundcloud.com/connect"
     public static var tokenUrl       = "\(baseUrl)/oauth2/token"
     public static var scope          = Set([] as [String])
-    public static var redirectUrl    = "http://localhost/"
-    public static var accountType    = "SoundCloud"
+    public static var redirectUri    = "http://localhost/"
     public static var keyChainGroup  = "SoundCloud"
 
     static let errorResponseKey = "com.alamofire.serialization.response.error.response"
 
     fileprivate static let userDefaults = UserDefaults.standard
-    static func newManager() -> SessionManager {
-        let m = Alamofire.SessionManager()
-        if let token = APIClient.accessToken {
-            m.session.configuration.httpAdditionalHeaders = ["Authorization": "Bearer \(token)"]
-        }
-        return m
-    }
-
-    static var oauth2clientDelegate: OAuth2ClientDelegate!
-    static func createOauth2client(_ delegate: OAuth2ClientDelegate) -> NXOAuth2Client? {
-        if let accessToken = account?.accessToken {
-            return NXOAuth2Client(clientID: clientId,
-                              clientSecret: clientSecret,
-                              authorizeURL: URL(string: authUrl)!,
-                                  tokenURL: URL(string: tokenUrl)!,
-                               accessToken: accessToken,
-                             keyChainGroup: keyChainGroup,
-                                persistent: true,
-                                  delegate: delegate)
-        } else {
-            return nil
-        }
-    }
-
-    fileprivate static var _account: NXOAuth2Account?
-    public static var account: NXOAuth2Account? {
-        if let a = _account {
-            return a
-        }
-        let store = NXOAuth2AccountStore.sharedStore() as! NXOAuth2AccountStore
-        for account in store.accounts as! [NXOAuth2Account] {
-            if account.accountType == APIClient.keyChainGroup {
-                _account = account
-                return account
-            }
-        }
-        return nil
+    static var oauth: OAuth2Swift!
+
+    static var credential: OAuthSwiftCredential? {
+        get {
+            return KeychainPreferences.sharedInstance[APIClient.keyChainGroup] as? OAuthSwiftCredential
+        }
+        set {
+            KeychainPreferences.sharedInstance[APIClient.keyChainGroup] = newValue
+        }
     }
 
     fileprivate static var _me: User?

                    observer.sendCompleted()
                }
            }
            self.fetch(route, callback: callback)
        }
    }

    func fetchItems<T: JSONInitializable>(_ route: Router) -> SignalProducer<[T], NSError> {
        return SignalProducer { observer, disposable in
            let callback: OAuth2ClientDelegate.RequestCallback = {(response: DataResponse<Any
                >) -> Void in
                if let e = response.result.error as NSError? {
                    if let r = response.response {
                        observer.send(error: NSError(domain: e.domain, code: e.code, userInfo: [APIClient.errorResponseKey:r]))

    }

    public static func clearAllAccount() {
        credential = nil
        SoundCloudKit.APIClient.accessToken = nil
    }

    public class func setup() {
        loadConfig()
        oauth = OAuth2Swift(
            consumerKey:    clientId,
            consumerSecret: clientSecret,
            authorizeUrl:   authUrl,
            accessTokenUrl: tokenUrl,
            responseType:   "code"
        )
        if let c = credential {
            oauth.client.credential.oauthToken          = c.oauthToken
            oauth.client.credential.oauthTokenSecret    = c.oauthTokenSecret
            oauth.client.credential.oauthTokenExpiresAt = c.oauthTokenExpiresAt
            oauth.client.credential.oauthRefreshToken   = c.oauthRefreshToken
            APIClient.sharedInstance.manager.session.configuration.httpAdditionalHeaders = ["Authorization": "Bearer \(c.oauthToken)"]
        }
        APIClient.sharedInstance.manager.retrier = SoundCloudOAuthRequestRetrier(oauth)
        APIClient.accessToken = credential?.oauthToken
    }

    static func authorize(_ viewController: UIViewController, callback: (() -> ())? = nil) {
        let vc = OAuthViewController()
        viewController.addChildViewController(vc)
        oauth.authorizeURLHandler = vc
        let _ = oauth.authorize(
            withCallbackURL: URL(string: APIClient.redirectUri)!,
            scope: APIClient.scope.joined(separator: ","),
            state: "SoundCloud",
            success: { credential, response, parameters in
                APIClient.credential = credential
                APIClient.accessToken = credential.oauthToken
                APIClient.sharedInstance.fetchMe().on(
                    failed: { error in
                        if let callback = callback { callback() }
                }, value: { user in
                    APIClient.me = user
                    AppDelegate.shared.reload()
                    if let callback = callback { callback() }
                }).start()
        },
            failure: { error in
                if let callback = callback { callback() }
        })
    }

    public class func loadConfig() {
