Refactoring toJSON