Updated code and annoted slightly what is going on