Implemented toJSON and added tests