Adds comments and does a little refactoring