BUG: fix some bugs; add some tests

Closes #123 

Along the way, I added some tests and found some small bugs having to do with modification of mappings that are essentially global.  I fixed those bugs as well, which is why this commit grew so big.