                 
                 for y in 0..<Int(inputImageHeight) {
                     for x in 0..<Int(inputImageWidth) {
-                        let pixelInfo: Int = ((Int(inputImageWidth) * Int(y)) + Int(x)) * numberOfComponents
+                        let pixelInfo: Int = ((Int(bytesPerRow) * Int(y)) + Int(x * numberOfComponents))
                         
                         if data[pixelInfo] == UInt16(label) {
                             minX = min(minX, x)

            let numberOfComponents = CGImageGetBitsPerPixel(cgImage) / CGImageGetBitsPerComponent(cgImage)
            
            //data <- bitmapData
            
            let inputImageHeight = inputImage.size.height
            let inputImageWidth  = inputImage.size.width
            
            var data = [UInt16](count: (Int(inputImageWidth) * Int(inputImageHeight)) * numberOfComponents, repeatedValue: 0)
            
            for dataIndex in 0..<data.count {
                data[dataIndex] = UInt16(bitmapData[dataIndex])

            
            for y in 0..<Int(inputImageHeight) {
                for x in 0..<Int(inputImageWidth) {
                    let pixelInfo: Int = ((Int(bytesPerRow) * Int(y)) + Int(x * numberOfComponents))
                    let pixelIndex:(Int, Int) -> Int = {x, y in
                        return ((Int(bytesPerRow) * Int(y)) + Int(x * numberOfComponents))
                    }
                    
                    if data[pixelInfo] == 0 { //Is Black
