More refactoring and optimizing.