cleaned up sort code