Refactoring, optimizations