   }
   
   private func controlCapacity() {
-    if size <= capacity {
-      return
-    }
-    
-    let cachePath = path
-    enumerateContentsOfDirectorySortedByAscendingModificationDateAtPath(cachePath) { (URL, _, inout stop: Bool) in
-      if let path = URL.path {
-        self.removeFileAtPath(path)
-        stop = self.size <= self.capacity
+    if size > capacity {
+      enumerateContentsOfDirectorySortedByAscendingModificationDateAtPath(path) { (URL, inout stop: Bool) in
+        if let path = URL.path {
+          removeFileAtPath(path)
+          stop = size <= capacity
+        }
       }
     }
   }
   
-  private func setDataSync(data: NSData, key : String) {
+  private func setDataSync(data: NSData, key: String) {
     let path = pathForKey(key)
-    let previousAttributes : NSDictionary? = fileManager.attributesOfItemAtPath(path, error: nil)
-    let success = data.writeToFile(path, options: NSDataWritingOptions.AtomicWrite, error: nil)
-    if !success {
+    let previousAttributes: NSDictionary? = fileManager.attributesOfItemAtPath(path, error: nil)
+    if !data.writeToFile(path, options: .AtomicWrite, error: nil) {
       Logger.log("Failed to write key \(key) on the disk cache")
     }
     
-    if let attributes = previousAttributes {
-      size -= attributes.fileSize()
-    }
-    size += UInt64(data.length)
+    size += (UInt64(data.length) - (previousAttributes?.fileSize() ?? 0))
     
     controlCapacity()
   }
   
-  private func updateDiskAccessDateAtPath(path : String) -> Bool {
+  private func updateDiskAccessDateAtPath(path: String) -> Bool {
     return fileManager.setAttributes([
       NSFileModificationDate: NSDate()
     ], ofItemAtPath: path, error: nil)
   }
   
   private func removeFileAtPath(path: String) {
-    if let attributes : NSDictionary = fileManager.attributesOfItemAtPath(path, error: nil) {
-      let fileSize = attributes.fileSize()
-      if fileManager.removeItemAtPath(path, error: nil) {
-        size -= fileSize
-      }
+    if let attributes: NSDictionary = fileManager.attributesOfItemAtPath(path, error: nil)
+       where fileManager.removeItemAtPath(path, error: nil) {
+      size -= attributes.fileSize()
     }
   }
 }
 
-private func enumerateContentsOfDirectorySortedByAscendingModificationDateAtPath(path: String, usingBlock block : (NSURL, Int, inout Bool) -> Void) {
+private func enumerateContentsOfDirectorySortedByAscendingModificationDateAtPath(path: String, @noescape usingBlock block: (NSURL, inout Bool) -> Void) {
   let property = NSURLContentModificationDateKey
-  let directoryURL = NSURL(fileURLWithPath: path)
-  if directoryURL == nil { return }
-  var error : NSError?
-  if let contents = NSFileManager.defaultManager().contentsOfDirectoryAtURL(directoryURL!, includingPropertiesForKeys: [property], options: NSDirectoryEnumerationOptions.allZeros, error: &error) as? [NSURL] {
-    
-    let sortedContents = contents.sorted({(URL1 : NSURL, URL2 : NSURL) -> Bool in
-      var value1 : AnyObject?
+  if let directoryURL = NSURL(fileURLWithPath: path),
+     let contents = NSFileManager.defaultManager().contentsOfDirectoryAtURL(directoryURL, includingPropertiesForKeys: [property], options: .allZeros, error: nil) as? [NSURL] {
+    let sortedContents = contents.sorted({ (URL1, URL2) in
+      var value1: AnyObject?
       if !URL1.getResourceValue(&value1, forKey: property, error: nil) {
         return true
       }
-      var value2 : AnyObject?
+      
+      var value2: AnyObject?
       if !URL2.getResourceValue(&value2, forKey: property, error: nil) {
         return false
       }

  }
  
  private lazy var cacheQueue : dispatch_queue_t = {
    let queueName = CarlosGlobals.QueueNamePrefix + self.path.lastPathComponent
    let cacheQueue = dispatch_queue_create(queueName, nil)
    return cacheQueue
  }()
  
  public func onMemoryWarning() {}

      return false
    })
    
    for value in sortedContents {
      var stop = false
      block(value, &stop)
      if stop {
        break
      }
    }
  }
}