refactored to_json method