Refactor into functions and classes.