Fix crash bug when deleting first line in document

+ Refactoring