added more code explanations and thoughts