add comments and few changes in logic