added comments and explanation