Add more tests and fix toJSON