updated code with comments