I added comments that should help understand it. Key is in the picture :)