More refactoring, speed improvements