Refactor into classes, fix and clean up tests