                         emptyListItem = lastLine.match /^(\s*)(\d+)\.\s$/
 
                         if emptyListItem
-                            markdownCaretPositon = removeEmptyLine(data.textarea, lines.length - 1, data.caretPosition)
+                            markdownCaretPositon = addLine(data.textarea, cursorLine - 1)
                         else
-                            breakLineAtBeginning = newLineContent.match /^(\s*)(\d+)\.\s/
-
-                            if !breakLineAtBeginning
-                                return "\n#{match[1] + (parseInt(match[2], 10) + 1)}. "
-
-                    return "\n"
-
-                afterInsert: (data) ->
-                    # Calculate the scroll position
-
-                    if markdownCaretPositon
-                        setCaretPosition(data.textarea, markdownCaretPositon)
-                        caretPosition = markdownCaretPositon
-                        markdownCaretPositon = false
-                    else
-                        caretPosition = data.caretPosition
-
-                    totalLines = data.textarea.value.split("\n").length
-                    line = data.textarea.value[0..(caretPosition - 1)].split("\n").length
-                    scrollRelation = line / totalLines
-                    $el.scrollTop((scrollRelation * $el[0].scrollHeight) - ($el.height() / 2))
+                            markdownCaretPositon = addLine(data.textarea, cursorLine, "#{match[1] + (parseInt(match[2], 10) + 1)}. ")
+
+
+                    setCaretPosition(data.textarea, markdownCaretPositon) if markdownCaretPositon
 
             markupSet: [
                 {

                        emptyListItem = lastLine.match /^(\s*\* )$/

                        if emptyListItem
                            markdownCaretPositon = removeEmptyLine(data.textarea, lines.length - 1, data.caretPosition)
                        else
                            breakLineAtBeginning = newLineContent.match /^(\s*)\*\s/

                            if !breakLineAtBeginning
                                return "\n#{match[1]}" if match

                    # ordered list
                    match = lastLine.match /^(\s*)(\d+)\.\s/

                    markdown.off(".preview")
                    closePreviewMode()

        setCaretPosition = (textarea, caretPosition) ->
            if textarea.createTextRange
                range = textarea.createTextRange()
                range.move("character", caretPosition)
                range.select()

            else if textarea.selectionStart
                textarea.focus()
                textarea.setSelectionRange(caretPosition, caretPosition)

            # Calculate the scroll position
            totalLines = textarea.value.split("\n").length
            line = textarea.value[0..(caretPosition - 1)].split("\n").length
            scrollRelation = line / totalLines
            $el.scrollTop((scrollRelation * $el[0].scrollHeight) - ($el.height() / 2))

        addLine = (textarea, nline, replace) ->
            lines = textarea.value.split("\n")

            if replace
                lines[nline] = replace + lines[nline]
            else
                lines[nline] = ""

            cursorPosition = 0

            for line, key in lines
                cursorPosition += line.length + 1 || 1

                break if key == nline

            textarea.value = lines.join("\n")

            #return the new position
            if replace
                return cursorPosition - lines[nline].length + replace.length - 1
            else
                return cursorPosition

        markdownSettings =
            nameSpace: "markdown"
            onShiftEnter: {keepDefault:false, openWith:"\n\n"}
            onEnter:
                keepDefault: false,
                replaceWith: () -> "\n"
                afterInsert: (data) ->
                    lines = data.textarea.value.split("\n")
                    cursorLine = data.textarea.value[0..(data.caretPosition - 1)].split("\n").length
                    newLineContent = data.textarea.value[data.caretPosition..].split("\n")[0]
