final refactor code