added more explanations in comments