added some explanations to the code