optimizations, and refactoring