Added some more tests and corrected a few bugs after getting clarifications from Jim