Added sorted function and added some comments