Even even more refactoring and optimizations