added more comments and explanations