Optimizations and light refactoring