updated sorting - nearly working