Move code into classes, add doctests