added some more comments explaining the code