added a bunch of comments and cleaned the code so it is easier to read