Optimize and refactor