 extension UIImage {
     
     class func animatedImageWithData(data: NSData) -> UIImage? {
-        let source = CGImageSourceCreateWithData(data, nil)
-        let image = UIImage.animatedImageWithSource(source)
-        
-        return image
+        guard let source = CGImageSourceCreateWithData(data, nil) else { return nil }
+
+        return UIImage.animatedImageWithSource(source)
     }
     
     class func delayForImageAtIndex(index: Int, source: CGImageSource!)

        var frame: UIImage
        var frameCount: Int
        for i in 0..<count {
            frame = UIImage(CGImage: images[Int(i)])!
            frameCount = Int(delays[Int(i)] / gcd)
            
            for j in 0..<frameCount {
                frames.append(frame)
            }
        }

        // Fill arrays
        for i in 0..<count {
            // Add image
            if let image = CGImageSourceCreateImageAtIndex(source, i, nil) {
                images.append(image)
            }
            
            // At it's delay in cs
            let delaySeconds = UIImage.delayForImageAtIndex(Int(i),
                source: source)
            delays.append(Int(delaySeconds * 1000.0)) // Seconds to ms
        }
