moved sorting function to bottom