Refactored the code and added sorting