updated sorting code