CPU optimizations and refactoring