Optimizing and refactoring