                 return element.byteRange.location
             } else if element.range.length == diff {
                 return NSMaxRange(element.byteRange)
-            } else {
-                let endUTF8index = element.line.content.utf16.startIndex.advancedBy(diff)
-                    .samePositionIn(element.line.content.utf8)!
-                let byteDiff = element.line.content.utf8.startIndex.distanceTo(endUTF8index)
-                return element.byteRange.location + byteDiff
-            }
+            }
+            let endUTF8index = element.line.content.utf16.startIndex.advancedBy(diff)
+                                      .samePositionIn(element.line.content.utf8)!
+            let byteDiff = element.line.content.utf8.startIndex.distanceTo(endUTF8index)
+            return element.byteRange.location + byteDiff
         }
 
         var lines: [Line] {

                return element.range.location
            } else if element.byteRange.length == diff {
                return NSMaxRange(element.range)
            } else {
                let endUTF16index = element.line.content.utf8.startIndex.advancedBy(diff)
                .samePositionIn(element.line.content.utf16)!
                let utf16Diff = element.line.content.utf16.startIndex.distanceTo(endUTF16index)
                return element.range.location + utf16Diff
            }
        }

        /**
         Returns UTF8 offset from UTF16 offset
         - parameter location: UTF16 based offset of string
         - returns: UTF8 based offset of string
        */
        func byteOffsetFromLocation(location: Int) -> Int {
            if self.rangesAndLine.isEmpty {
                return 0
            }
            guard let index = rangesAndLine.indexOf({ NSLocationInRange(location, $0.range) }) else {

        }
        
        /**
        Returns UTF16 offset from UTF8 offset.

        - parameter byteOffset: UTF8-based offset of string.

        - returns: UTF16 based offset of string.
        */
        func locationFromByteOffset(byteOffset: Int) -> Int {
            if rangesAndLine.isEmpty {
                return 0
            }
            guard let index = rangesAndLine.indexOf({ NSLocationInRange(byteOffset, $0.byteRange) }) else {
