added comments and updated code