Updated code with more comments