Added notes and explanations to code