refactor code & add comments