Bugfix. Sorted changed to sort
Bugfix. length changed to half_length