Added some comments and improvements to the code.