fix bug in calling comparator with undefined; add toJSON methods