Commented the code for easier comphrension.