Add more tests and fix toJSON

/to @ritch