added some comments and tried to understand all the stuff. Also tried to make it pretier, but did not finish 100%...