Added code comments and a little refactoring.