reformated the code and added some comments