Implemented test cases, plus some small fixes.
merging in @markhildreth's tests and cleaning up