added some comments and enhanced code readability