Minor refactoring by putting more of the logic inside classes.