Sorting implemented. Still very messy,