Several bug fixes and some refactoring.  There were copy paste bugs and flipped argumest.  Still WIP because tests are failing.