Added mutex to lock sort() and avoid segfault