Minor optimizations, refactoring