Some refactoring, JSON-friendly toMap() method.