Add comments and do a little refactoring