             restoreFromUserDefault(key: "valid_through")
         }
         
-        
         //checks for token existance and expiration date
         
-        if (self.token.allKeys.count == 0 ) { // // if user have to authorize with user/password
+        if self.token.allKeys.count == 0 { // // if user have to authorize with user/password
 
             NotificationCenter.default.post(name:.oAuthNeedsUserLogin, object: nil, userInfo: nil)
             
-        }else if (self.token.allKeys.count != 0  &&  (self.token["valid_through"] as! Date).isPassed() ) { // has a token, just have to refresh it
+        } else if let validThrough = self.token["valid_through"] as? Date, validThrough.isPassed() { // has a token, just have to refresh it
+
             refreshToken()
 
-        }else{// has a valid token
+        } else if let validThrough = self.token["valid_through"] as? Date, !validThrough.isPassed() { // has a valid token
+
             NotificationCenter.default.post(name:.tokenDidAuthorize, object: self.token, userInfo: nil)
+
         }
-    
     }
 }

                
                }
                
                let valid_through = requestDate.addingTimeInterval(TimeInterval.init(self.token["expires_in"] as! Int))
                
                self.token.setValue(valid_through, forKey: "valid_through") //using a Date to token validation keep it more simple
                UserDefaults.standard.setValue(self.token.value(forKey: "valid_through"), forKey: "valid_through")

                
                NotificationCenter.default.post(name:.tokenDidAuthorize, object: JSONReturn, userInfo: nil)



                print("tokenDidAuthorize \(JSONReturn)")
                
                if self.token.allKeys.count == 0 { // this is for the first access token request

                    self.token = NSMutableDictionary.init(dictionary: JSONReturn)
                    
