sorted functions and added some comments