Added some doctests and modularized things a bit.