Finished logic.  Added tests.