refactor code with comments