Refactor into classes (and remove a lot of junk)