Implement toDBObject() method.
Misc refactorings.