added comments and did some code cleaning