added iFlag, avoided unnecessary sorting steps