Fixed everything through sorting.

Sorting still gives a null pointer exception