Added comments and did a little refactoring