Added some comments and did a little refactoring.