Updated code with comments