Cleaned up a bit. Added tests.