Added some comments and formatted the code.