added local sort check. added error check