Optimize and refactor utils