refactored and added sort but nto fully working