Clean-up of sorting, filtering code