Added global sort check. Some refactoring. Code normalized.