David's unit tests, rebased and updated