Implemented sortingfunction, some housekeeping