added Matteo's functions to order the data, working!