refactor codes that split each function into functions.