Major refactoring (not functional yet.)