code commented and some code improvement