Fixed bug in data manipulation (d_sorted was used instead of d)