Comments added for better understanding.