changed order of functions and added some comments