added sorting function