Added comments and some code cleaning