optimization and refactor