some functions refactor and optimize