added some comments and formated code