Sort sections
Refactoring: add markWholeDocumentDirty and use it